Projectile Trajectory motion in Unity 2D

What is a Projectile and Projectile Motion?

When gravity force is acting on an object is called projectile. Other forces are also acting upon it but those all are minimal effect on a projectile object. The motion creates by the projectile to coming back to the surface is called projectile motion. This motion is angular and have their dimensions.

Types of Projectile Motion

Two dimension motion is more complex than one dimension motion
since the velocities can point in diagonal directions.

For example, a ball could be moving both horizontally and vertically at the same time with a diagonal velocity “u”. We break up the velocity vector, “u”, of the ball into two separate horizontal, “ux”​, and vertical, “uy”​, directions to simplify our calculations. so, there are two types of projectile motion.

  • Horizontal Motion
  • Vertical Motion
projection motion in unity 2d
projection motion in unity 2d

Horizontal motion

Since there is no force acting in the horizontal direction, X component of velocity is constant and acceleration in X direction is zero.

Displacement in X direction = initial velocity(u) * Time(t)

Vertical motion

A mainly active force in the vertical direction is gravity. Acceleration in this direction is opposite to the gravity. hence ay = -g.

The vertical component of the velocity changes linearly. so the component of the velocity in the y-direction is following.

velocity Y component = Gravity(g) * Time(t) and the displacement is

Displacement in Y direction = 1/2 * g* t * t

So here we discussed the angular projectile motion, it is largely depends on the trigonometric formula for calculating velocity component and angle of projectile launch.

Projectile angular motion formula
Projectile angular motion formula

Implementation in C#

Above, we derive the formula of projectile motion. It has two dimension to act upon x and y dimension. Both dimensions have their own formula which corresponds to axis x and y in Unity.

In both formula Velocity, angle and time is the common thing. Velocity is calculated by the following Pythagoras theorem,

Velocity(u) = Mathf. Sqrt((x * x) + (y * y));

The second thing is time. Time is change as the projectile is going ahead. Time is not constant. In unity, we will use the Time.deltatime or we can define our own time and timely add some float value.

Now for calculating the angle, we use trigonometric formula and velocity component together.

Angle = Mathf.Atan2(y, x);

We have calculated all the necessary thing for trajectory displacement. This all is combined following,

 void SetTrajectoryPoints(Vector3 posStart, Vector2 direction)
          float velocity = Mathf.Sqrt((direction.x * direction.x) + (direction.y * direction.y));

            float angle = Mathf.Rad2Deg * (Mathf.Atan2(direction.y, direction.x));

            float tTime = 0;

            tTime += 0.1f;

            foreach (Transform dot in trajectoryDots)
                float dx = velocity * tTime * Mathf.Cos(angle * Mathf.Deg2Rad);
                float dy = velocity * tTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * tTime * fTime / 2.0f);
                Vector3 pos = new Vector3(posStart.x - dx, posStart.y - dy, 0);
                dot.position = pos;
                dot.eulerAngles = new Vector3(0, 0, Mathf.Atan2(direction.y - (Physics.gravity.magnitude) * tTime, direction.x) * Mathf.Rad2Deg);
                tTime += 0.1f;

This is how you can use projectile trajectory in your games. You can download the full demo and use in your games for free. You can also try to implement this demo using c# job system.

Leave a Reply