3D trajectoiry prediction miscalculations

Hello, i’m trying to render a line on the trajectory that my object will take, i have made a sample code that works but is really unprecise, i have followed some tutorial but many of them don’t take in count the mass of the object and the drag so i hat to improvise, here is my code:

``````Vector3 calculate(Vector3 velocity, float time, Vector3 position,Rigidbody body)
{
Vector3 Vxz = velocity;
Vxz.y = 0f;
float drag = 1f - (time * body.drag);
Vector3 result = position + velocity * time;
result.y=  ((-0.5f * Math.Abs(Physics.gravity.y) * (time * time)) + (velocity.y * time) + position.y) *drag;
return result;
}
``````

I call this function for every point of the line and it gives a rough idea of where the object will be going, usually the object goes less further than the line, does anybody know what is wrong?

Well, you’re only applying the drag at result.y. Drag brakes Rigidbody at all directions, thats why your gameobject goes less further than your calcs. Try with this:

``````    Vector3 Calculate(Vector3 velocity, float time, Vector3 position, Rigidbody body)
{
float drag = 1f - (time * body.drag);
if (drag < 0) drag = 0;
Vector3 result = ((0.5f * Physics.gravity * Mathf.Pow(time, 2)) + (velocity * time) + position) * drag;
return result;
}
``````

I don’t know what Vector3 Vxz was doing there, so I removed it. Math.Abs takes an extra calculation, and would have some issue if you change your gravity to a positive number. Gravity by default is negative, so with a minus sign at the start of the multiplication is enough.
Since Physics.gravity at x and z is by default 0… the formula will keep as your initial one (position + velocity * time) at this axis, but with the drag applied. Also if you like to change the gravity of your environment, calcs will be right.

Forgot to say you that Rigidbody mass doesn’t affect to the path followed. It only affects to how much velocity the Rigidbody gets when a force is applied… So this tutorial you followed are right, we don’t have to take it in count if we know the velocity.
For a velocity calculation if you applies a ForceMode.Impulse, the initial velocity is the acceleration at Newton formulations:
rb.velocity = Vector3 force / rb.mass;