I want to create a projectile that fires upwards and in a sense falls down onto the player, like this:

enter image description here

I found through some research I needed to use a "parabolic" equation. However it wasnt very clear how to implement this.

Also a lot of the posts used these 4 variables

  • Gravitational Constant
  • Time to hit target
  • Initial pos
  • End pos

However these are the variables I have

  • Gravitational Constant (marked G)
  • Force upwards (marked T, measured as a number not in specific units) (for example if I want the projectile to go much higher and then fall down much more vertically)
  • Start pos (marked a)
  • End pos (marked b)

enter image description here

Can someone please clarify how I can do this in Unity? (Without telling me some huge equation like every other post did)

  • \$\begingroup\$ The reason every other post used an equation is that the equation is how you do this. People don't use it for giggles: it's what gets the job done. A force upwards in no particular units doesn't make sense as an input parameter. What you could use instead is an angle, describing how shallow or steep you want the arc to be at the point of takeoff. Other ways you could control this is to specify a launch speed, or a multiple of the "lowest energy" trajectory, where 1.0 is the weakest lob that can hit the target, and 2.0 launches twice as fast (more velocity upwards for a steeper arc). \$\endgroup\$
    – DMGregory
    Commented Mar 12 at 22:35
  • \$\begingroup\$ I understand the equation is needed to do it, but I don't understand how to actually use the equation in Unity using actual floats and math operations. In terms of the angle, I'm not quite sure how that would work, being as the angle would change depending on where the player is, thats why I just wanted some kind of float that controllers how upwards it goes. \$\endgroup\$
    – Pow
    Commented Mar 13 at 10:17
  • \$\begingroup\$ Do you have to use physics to do this? Or are you happy with the bullet just following an arc determined at the point of being fired, without any physics involved? \$\endgroup\$ Commented Mar 13 at 10:33
  • \$\begingroup\$ The bottom of this answer shows C# code you can paste into Unity without deriving it from the equation yourself. If you like the multiplier I proposed above, you can skip the lines mentioning b, discriminant, discRoot, T_min, and T_max and just use T = T_lowEnergy * multiplier. At the end this gives you the launch velocity of the projectile you can assign to its body directly. Or if you need it as an impulse, that's (launchVelocity - currentVelocity) * mass. \$\endgroup\$
    – DMGregory
    Commented Mar 13 at 10:35
  • \$\begingroup\$ The T you calculate here is total time airborne (in seconds), so you can clamp this below some max if you want to make sure the lob isn't too slow when the object is far away. In other answers I also show how to parametrize this by the altitude of the apex of the arc, if you'd like to control the arc height that way. \$\endgroup\$
    – DMGregory
    Commented Mar 13 at 10:38

1 Answer 1


Unity will actually do all of the simulation for you. Given you are providing the vertical component \$T\$ of the force, all you need is the horizontal component \$\vec{H}\$, So all you need to do is re-arrange the equations so that, given your inputs, you get that component:

$$\vec{F} = \vec{H} + T \vec{up}$$

In Unity: Vector3 F = H + T * Vector3.up;. With that you can apply the force in Unity with rigidbody.AddForce(F, ForceMode.Impulse);.

To get the horizontal force \$\vec{H}\$, you will need the mass of your projectile \$m\$ and the horizontal velocity \$\vec{h}\$ that you want to achieve:

$$\vec{H} = \vec{h}m$$

The mass you can get from rigidbody.mass but the horizontal velocity you will need to calculate, and for that, you will need to know the travel time \$t\$ and the vector \$\vec{AB}\$ from \$a\$ to \$b\$:

$$\vec{h} = \frac{\vec{AB}}{t}$$

You can find \$\vec{AB}\$ in Unity with Vector3 AB = b - a. But to find the travel time \$t\$ (how long the projectile is in the air) you need to know the upward velocity \$u\$ and your gravity \$g\$:

$$t = \frac{2u}{g}$$

Nearly there. To find the upward velocity \$u\$ you need again the mass \$m\$ of your projectile and the upward force \$T\$ that you are supplying:

$$u = \frac{T}{m}$$

And so finally your code would be:

        float u = T / rigidbody.mass;
        float t = 2 * u / Physics.gravity.magnitude;
        Vector3 AB = b - a;
        Vector3 h = AB / t;
        Vector3 H = h * rigidbody.mass;
        Vector3 F = H + T * Vector3.up;
        rigidbody.AddForce(F, ForceMode.Impulse);
  • \$\begingroup\$ Thanks for your answer, I'll test it out later and see how it goes. \$\endgroup\$
    – Pow
    Commented Mar 13 at 14:59
  • \$\begingroup\$ It works perfectly, thankyou! \$\endgroup\$
    – Pow
    Commented Mar 13 at 22:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .