Answers for "unity3d predict location of target"

C#
0

unity3d predict location of target

private Vector3 predictedPosition(Vector3 targetPosition, Vector3 shooterPosition, Vector3 targetVelocity, float projectileSpeed)
		{
            Vector3 displacement = targetPosition - shooterPosition; 
			float targetMoveAngle = Vector3.Angle(-displacement, targetVelocity) * Mathf.Deg2Rad;
			//if the target is stopping or if it is impossible for the projectile to catch up with the target (Sine Formula)   
			if (targetVelocity.magnitude == 0 || targetVelocity.magnitude > projectileSpeed && Mathf.Sin(targetMoveAngle) / projectileSpeed > Mathf.Cos(targetMoveAngle) / targetVelocity.magnitude) {
				Debug.Log("Position prediction is not feasible.");             
				return targetPosition;        
			}
			//also Sine Formula 
			float shootAngle = Mathf.Asin(Mathf.Sin(targetMoveAngle) * targetVelocity.magnitude / projectileSpeed);
			return targetPosition + targetVelocity * displacement.magnitude / Mathf.Sin(Mathf.PI - targetMoveAngle - shootAngle) * Mathf.Sin(shootAngle) / targetVelocity.magnitude;     
		}
Posted by: Guest on November-21-2020

C# Answers by Framework

Browse Popular Code Answers by Language