trajectory prediction

Hi everyone. I’m trying to draw the trajectory that the projectile has to do. I searched the net and came to this conclusion. The problem is that the drawing trajectory does not respect the launching trajectory. Can you help me?

using UnityEngine;

public class BunnyController : MonoBehaviour
{
    private readonly Vector2 LAUNCH_VELOCITY = new Vector2(20f, 80f);
    private readonly Vector2 INITIAL_POSITION = Vector2.zero;
    private readonly Vector2 GRAVITY = new Vector2(0f, -240f);
    private const float DELAY_UNTIL_LAUNCH = 1f;
    private int NUM_DOTS_TO_SHOW = 30;
    private float DOT_TIME_STEP = 0.05f;

    private bool launched = false;
    private float timeUntilLaunch = DELAY_UNTIL_LAUNCH;
    private Rigidbody2D rigidBody;

    public GameObject trajectoryDotPrefab;

    private void Awake()
    {
        rigidBody = GetComponent<Rigidbody2D>();
    }

    private void Start()
    {
        for (int i = 0; i < NUM_DOTS_TO_SHOW; i++)
        {
            GameObject trajectoryDot = Instantiate(trajectoryDotPrefab);
            trajectoryDot.transform.position = CalculatePosition(DOT_TIME_STEP * i);
        }
    }

    private void Update()
    {
        timeUntilLaunch -= Time.deltaTime;

        if (!launched && timeUntilLaunch <= 0)
        {
            Launch();
        }
    }

    private void Launch()
    {
        rigidBody.velocity = LAUNCH_VELOCITY;

        launched = true;
    }

    private Vector2 CalculatePosition(float elapsedTime)
    {
        return GRAVITY * elapsedTime * elapsedTime * 0.5f + LAUNCH_VELOCITY * elapsedTime + INITIAL_POSITION;
    }
}

It looks like the topic was covered in the thread Drawing projectile trajectory
It seems HappyMoo came with something that does the job there.

I may add that Sebastian Lague made a great video game physics series on youtube, covering the kinematic equasions used in jumping, firing projectiles, calculating the time something needs to hit an object, calculating the velocity an object needs to be thrown at to arrive at a specific point, and some more.
The latter also includes drawing a line for that object, which applies here.

2 Likes

the problem is that I need to launch the projectile with a rigid body force without knowing what its target will be. A reference target is used in the video

Yeah, but in his case that’s what he wanted to do. It’s more about the equations involved than the path drawing, but you can use the kinematic equations to get the sample points from which you draw your line.
You have an initial velocity, you have acceleration due to gravity and you have a time (sample rate).
Thus you should have enough information to solve for all other values in the kinematic equations. You want to solve the equation for displacement, to know at which point your ball will be after a specific time, with a specific initial velocity and a downwards acceleration due to gravity. You then repeat this process to get multiple positions for multiple time values (your sample time) and either draw dots or connect them to a line, in order to get your output line.

Or in other words: to get the point where your object will be in t seconds on its trajectory, you need to add to your position your velocity times t, but also account for changes in velocity due to acceleration (gravity here) over time t.
And that’s exactly what the kinematic equations should allow you to do.
If you plan on watching the video series i linkes, i’d probably start by watching 1 to get a grasp of what you are doing.

Other code specific things i noted in your example:

  • you are using your own GRAVITY constant, but only when calculating the path. Unless you set that as the global gravity constant, your rigidbody does not use this value, thus a difference in the result is to be expected.

  • i’m pretty sure you are not supposed to directly set the rigidbody velocity, but rather apply a force to a rigidbody using AddForce() - in your case with the ForceMode.Impulse to launch an object.

Here’s short and nicely done tutorial with trajectory prediction:

1 Like

Just thought I’d add this nice none math intensive but performance intensive method to the thread for those that may want to predict objects that will could potentially collide with multiple objects. It can be quite hard to account for multiple collisions using trig and manual physics.