This site uses strictly necessary cookies. More Information

X- Home /

# 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. Thanks in advance

**Answer** by ModLunar
·
Aug 01, 2018 at 04:46 PM

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
projectile.AddForce(force);
//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
);
}
```

**Answer** by madks13
·
Jul 31, 2018 at 03:29 PM

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.

I know what you're trying to say but sometimes those formulas can be challenging to derive yourself, and even then, they can be physically correct (accurate, just as you learned in your physics class) but still deviate from the result that the physics engine comes up with, and you won't even know why.

I've tried to pre-calculate the max height that my character will jump given the constant acceleration of gravity in Physics.gravity and my initial y-velocity, and my calculations are always a little off and I don't know why :(

Oh not necessarily, I'll see if I can come up with a formula. It might get you a close approximation at least

**Answer** by coolbudy1998
·
Aug 01, 2018 at 04:40 PM

Hey bro, you can check out this link- http://www.theappguruz.com/blog/display-projectile-trajectory-path-in-unity. 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!!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.