Search code examples
unity-game-engine2dunity3d-2dtoolsprojectile

Calculating 2D parabolic trajectory for a projectile to hit a position


I'm developing a Tower defense in 2D in Unity.

I'm trying to calculate a parabola to draw the trajectory of the projectiles fired from a tower in the game. I've got the position of the tower and the position of the enemy - now I need an algorithm that calculates a parabola that will hit the enemy.

Is there a universal algorithm or something like that for this kind of calculation?


Solution

  • Assuming gravity but no drag we have:

    where

    • x, y is displacement
    • x_0, y_0 is initial location
    • u_x, u_y is initial velocity
    • g is acceleration due to gravity
    • t is time elapsed