# find the exact point on which a projectile will hit enemy

Hey everybody.
I was just wondering if there is anyway to find the exact point on which a projectile will hit the enemy (head, lung, or other parts). In my game the enemies are far far away so the player has to aim above the enemy to hit him.
I,m using rigidbody and addforce for my projectile motion, and plus raycast is not possible as raycast move in straight line.
I want to find it as soon as the projectile is launch or even before it actually hit the enemy.

So um I don’t have the time to explain everything about what I found, but here’s the gist of it:

• I assumed we’re on a big enough planet to approximate that gravity only acts in the vertical y direction. (You can get the gravity vector in Unity as `Physics.gravity`, and `Physics.gravity.y` is by default -9.81 (m/s²)

• We’re dealing with projectile motion. In a physics class, this would be under a section usually called “Kinematics”, with no regard to forces – just displacement, velocity, and acceleration.

• This was (for me) a 2-step problem. First, “translating” the force applied to the object into what velocity that force would cause. Then, once I calculated the initial velocity, caused by the force (assuming the force happened nearly instantaneously, with only one call to rigidbody.AddForce(…)), I used kinematics equations to figure out its position after a given amount of time, based on that velocity it started with, and that gravity will be affecting (only) the y direction.

• If it doesn’t help you because time isn’t what you want to be plugging in these equations (maybe you don’t care about the time it takes to do this) you’ll have to take these formulas a step further for what you wanna do, and perhaps use a couple of raycasts to actually see if it’d collide with anything along its path.

• The first part where I had to get the initial velocity of the object based on the force you applied – this comes from Newton’s Second Law (F=ma)

Highlighted in yellow are the final vector formulas.

The most important one you want is Δr(t), which represents the change in position of the object over a certain time t in seconds after the launch. Gah I forgot the vector symbol above the r! Lol oh well. And v(t) represents the object’s velocity over that same time period. t = 0 when you just started the launch. t = 1 would be one second after the launch. These formulas are only valid until it hits something, because the entire problem, including its trajectory, changes after that.

Any details about what this thing would be colliding with, you’d use some kind of raycast, spherecast, boxcast, etc. from Unity’s UnityEngine.Physics class. These formulas (most notably Δr(t)) will help give you the path the object will travel. Here’s some quick and dirty code I wrote out, I did it in Notepad++ so hopefully I didn’t make any silly typos that’d make it not compile xD

``````private Rigidbody rigidbody;
private Vector3 initialPos;
private Vector3 initialVelocity;

private float startTime;

public void Start() {
projectile = GetComponent<Rigidbody>();

LaunchProjectile(new Vector3(500, 0, 0));
}

private void LaunchProjetile(Vector3 force) {
initialPos = projectile.position;
initialVelocity = force * Time.fixedDeltaTime / projectile.mass;
startTime = Time.time;

//AddForce(...) without the second parameter for the ForceMode
//means you used ForceMode.Force, which is the default

//Actually you might be able to just get the initialVelocity here with projectile.velocity... lol. But I'm not sure if its velocity gets calculated immediately... if anything, wait one FixedUpdate frame.
}

public void Update() {
float timeAfterwards = Time.time - startTime;
Debug.Log("Theoretical position at t = " + timeAfterwards + ": " + GetPositionAfterTime(timeAfterwards, initialPos, initialVelocity);
}

//time is in seconds
private Vector3 GetPositionAfterTime(float time, Vector3 initialPos, Vector3 initialVelocity) {
return new Vector3(
initialVelocity.x * time,
0.5f * Physics.gravity.y * time * time + initialVelocity.y * time,
initialVelocity.z * time
);
}
``````

Hey bro, you can check out this link-
Unity - How to display projectile trajectory path.
The bottom part of code shows a formula for velocity and angle of projectile (these two are the main characteristic feature of a projectile).
Understand it thoroughly and you are done!!

Yes there is, it’s called math. I don’t have the formula, but you should be able to find asset that will help you do that. Otherwise you will have to implement the formula yourself.