- Home /
How do i calculate a projectiles force by the distance to target?
Hello! I need to calculate how much force i need to apply to a projectile, to hit a target.
I have an arcing projectile, which is shot towards the closest player using Transform.LookAt(); and Vector3.Distance();, but I can't figure out how I should calculate to make the projectile always hit it's target, no matter what distance is between the "instantiator" and the target.
Here's a quick video demonstrating it in my game:
As you see, it doesn't calculate it properly.
The code:
time -= Time.deltaTime;
distanceToTarget = Vector3.Distance(transform.position, player.transform.position);
if(time <= 0)
{
snowballInstance = Instantiate (spawnObject, transform.position + transform.up, transform.rotation) as GameObject;
snowballInstance.rigidbody.AddForce(transform.forward * 250);
snowballInstance.rigidbody.AddForce(transform.up * distanceToTarget * 47.5f);
time = maxTime;
}
Thank you for your attention!
Answer by TargonStudios · Feb 09, 2015 at 12:38 AM
Here are some helpful links:
-http://answers.unity3d.com/questions/487065/calculate-a-rigidbody-trajectory.html
-http://answers.unity3d.com/questions/248788/calculating-ball-trajectory-in-full-3d-world.html
You can also try researching "Parabola".