How to make a Predicted trajectory path in Unity

In this tutorial, we will see how to make a predicted trajectory path using initial velocity, throw angle and Line Renderer. This method will work for both 2D and 3D but for the sake of this tutorial, we will demonstrate it in 3D.

Video tutorial

Setting up the scene

We have a ground and a Capsule added to the default Unity scene. The Capsule has a Rigidbody component attached to it. We are going to add force to make the Capsule jump in a parabolic path and we will be using a Line Renderer to predict the path.

Select the Capsule and add a Line Renderer component. Set the material of the Line renderer and adjust the size of the line to your requirement.

Make sure to uncheck “Use World Space”. We are ready for the script.

Unity Black Friday Deals

How Line Renderer Works

Line Renderer draws a line based on the points assigned to it. You can assign these points manually or assign them using script. We are going to calculate the points on the trajectory using a physics equation and set it to the Line Renderer.

Finding the Position (How the script works)

The first position of the line renderer will be the position of the game object. To calculate the remaining position, we will need the start velocity.

If you are going to throw an object with a force 20 then the initial velocity will be

Initial velocity=Force*direction/mass

In some cased we need to throw the object at an angle in the forward direction in that case. We can calculate the direction by multiplying the Forward vector with a Quaternion with the angle that we need. In this case, the initial velocity will be

Initial velocity=Quaternion rotation*forward direction*force/mass

Now with the initial velocity we can find the x and z position by multiplying the initial velocity with the time difference between each point. We don’t know the time between each point so we can have some value like 0.1 or 0.2 depending in how smooth you want the curve.

For the first point the position is equal to start position + time*initial velocity. The second position will be Start+time*2*initial velocity.

For the Y position we need to use the following formula

s=ut+0.5at2

Where,

s=distance, u=initial velocity, a =gravity, t= time

Here is the final script with all these taken care of

Trajectory Script

using UnityEngine;

public class PathProjection : MonoBehaviour
{
    LineRenderer lr;
    Rigidbody rb;
    Vector3 startPosition;
    Vector3 startVelocity;
    float InitialForce=15;
    float InitialAngle=-45;
    Quaternion rot;
    int i=0;
    int NumberOfPoints=50;
    float timer=0.1f;

    // Start is called before the first frame update
    void Start()
    {
        lr=GetComponent<LineRenderer>();
        rb=GetComponent<Rigidbody>();
        rot=Quaternion.Euler(InitialAngle,0,0);
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            drawline();
        }
        if(Input.GetKeyUp(KeyCode.Space))
        {
            rb.AddForce(rot*(InitialForce*transform.forward),ForceMode.Impulse);
            //lr.enabled=false;
        }
        
    }

    private void drawline()
    {
        i=0;
        lr.positionCount=NumberOfPoints;
        lr.enabled=true;
        startPosition=transform.position;
        startVelocity=rot*(InitialForce*transform.forward)/rb.mass;
        lr.SetPosition(i,startPosition);
        for(float j=0;i<lr.positionCount-1;j+=timer)
        {
            i++;
            Vector3 linePosition=startPosition+j*startVelocity;
            linePosition.y=startPosition.y+startVelocity.y*j+0.5f*Physics.gravity.y*j*j;
            lr.SetPosition(i,linePosition);
        }
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.