So i had a script that translated an object, at a certain angle and speed to a certain vector which faked the parabolic ark. But its not really what im looking for so i decided to try and calculate the angle and target properly(for a moving target). Im new to scripting but ive put these together from research on the net.
As far as i can tell(from debugs) im getting an angle in radians and im getting a vector3.
But now where do i go from here? i guess i use RigidBody.addforce? but how do i add the force at the angle and in the direction of the vector3?
Here’s the code if u wanna look.(ive never used maths in C# so if u can help me tidy it up that would help also)
public IEnumerator AngleOfShot()
{
// PREDICTS SHOT ON MOVING TARGET
target = FindClosestEnemy().gameObject;
//positions
Vector3 shooterPosition = shooter.transform.position;
Vector3 targetPosition = target.transform.position;
//velocities
Vector3 shooterVelocity = shooter.rigidbody ? shooter.rigidbody.velocity : Vector3.zero;
Vector3 targetVelocity = target.rigidbody ? target.rigidbody.velocity : Vector3.zero;
//calculate intercept
Vector3 interceptPoint = FirstOrderIntercept
(
shooterPosition,
shooterVelocity,
shotSpeed,
targetPosition,
targetVelocity
);
Vector3 distance = transform.position - interceptPoint;
float enemydistance = distance.sqrMagnitude;
float angleOfShot = (9.81 * Mathf.Sqrt(enemydistance) / (shotSpeed * shotSpeed));
float realAngle = Mathf.Sin(angleOfShot / 2);
Debug.Log (realAngle);
yield return new WaitForSeconds(3.0F);
}
// Sum i used for AngleofShot
