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?
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.
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.
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.