Esempio n. 1
0
		//Aiming used if part has a turret module
		void Aim()
		{
			//AI control
			if(aiControlled && !slaved)
			{
				if(legacyTargetVessel)
				{
					targetPosition += legacyTargetVessel.rb_velocity * Time.fixedDeltaTime;
				}
				else if(!targetAcquired)
				{
					autoFire = false;
					return;
				}
			}



			if(!slaved && !aiControlled && (yawRange > 0 || maxPitch-minPitch > 0))
			{
				//MouseControl
				Vector3 mouseAim = new Vector3(Input.mousePosition.x/Screen.width, Input.mousePosition.y/Screen.height, 0);
				Ray ray = FlightCamera.fetch.mainCamera.ViewportPointToRay(mouseAim);
				RaycastHit hit;
				if(Physics.Raycast(ray, out hit, maxTargetingRange, 557057))
				{
					targetPosition = hit.point;

					//aim through self vessel if occluding mouseray
					Part p = hit.collider.gameObject.GetComponentInParent<Part>();
					if(p && p.vessel && p.vessel == vessel)
					{
						targetPosition = ray.direction * maxTargetingRange + FlightCamera.fetch.mainCamera.transform.position; 
					}
				}
				else
				{
					targetPosition = (ray.direction * (maxTargetingRange+(FlightCamera.fetch.Distance*0.75f))) + FlightCamera.fetch.mainCamera.transform.position;	
					if(legacyTargetVessel!=null && legacyTargetVessel.loaded)
					{
						targetPosition = ray.direction * Vector3.Distance(legacyTargetVessel.transform.position, FlightCamera.fetch.mainCamera.transform.position) + FlightCamera.fetch.mainCamera.transform.position;	
					}
				}
			}


			//aim assist
			Vector3 finalTarget = targetPosition;
			Vector3 originalTarget = targetPosition;
			targetDistance = Vector3.Distance(finalTarget, transform.position);
			targetLeadDistance = targetDistance;
			if((BDArmorySettings.AIM_ASSIST || aiControlled) && eWeaponType!=WeaponTypes.Laser)
			{
				float gAccel = (float) FlightGlobals.getGeeForceAtPosition(finalTarget).magnitude;
				float time = targetDistance/(bulletVelocity);

				if(targetAcquired)
				{
					float time2 = VectorUtils.CalculateLeadTime(finalTarget-fireTransforms[0].position, targetVelocity-vessel.srf_velocity, bulletVelocity);
					if(time2 > 0) time = time2;
					finalTarget += (targetVelocity-vessel.srf_velocity) * time; //target vessel relative velocity compensation

					Vector3 acceleration = targetAcceleration;
					finalTarget += (0.5f * acceleration * time * time); //target acceleration
				}
				else if(vessel.altitude < 6000)
				{
					float time2 = VectorUtils.CalculateLeadTime(finalTarget-fireTransforms[0].position, -part.rb.velocity, bulletVelocity);
					if(time2 > 0) time = time2;
					finalTarget += (-part.rb.velocity*(time+Time.fixedDeltaTime));  //this vessel velocity compensation against stationary
				}
				Vector3 up = (finalTarget - vessel.mainBody.transform.position).normalized;
				if(bulletDrop && vessel.srfSpeed < 750) finalTarget += (0.5f*gAccel*time*time*up);  //gravity compensation
				
				targetLeadDistance = Vector3.Distance(finalTarget, fireTransforms[0].position);

				fixedLeadOffset = originalTarget-finalTarget; //for aiming fixed guns to moving target	

				
				//airdetonation
				if(airDetonation)
				{
					if(targetAcquired)
					{
						detonationRange = Mathf.Clamp(targetLeadDistance, 500, maxAirDetonationRange) - 50f;
					}
					else
					{
						detonationRange = defaultDetonationRange;
					}
				}
				
			}

			if(airDetonation)
			{
				detonationRange *= UnityEngine.Random.Range(0.95f, 1.05f);
			}

			finalAimTarget = finalTarget;

			//final turret aiming
			if(slaved && !targetAcquired) return;
			if(turret)
			{
				bool origSmooth = turret.smoothRotation;
				if(aiControlled || slaved)
				{
					turret.smoothRotation = false;
				}
				turret.AimToTarget(finalTarget);
				turret.smoothRotation = origSmooth;
			}
		}