# Raycasting Character Jump Trajectory

I’m in need of raycasting the trajectory my character will make after jumping, so that I’ll be able to know if my character will bump into anything while jumping and for other reasons.
I don’t need it to be super precise, I was thinking of using 4/5 raycasts so that they wouldn’t be too much of a strain.

I’ve been trying to get this working and I haven’t really done any progress with it since I really don’t understand how I should be doing this. Here’s an example of what I need

Here’s what I’ve been able to get so far by using some code that I found here Projectile prediction line? - Unity Engine - Unity Discussions

This seems like it’s close to working, however this is how it looks from the front

Here’s my code

``````    void calculateJumpTrajetory () {

int numSteps = 20; // for example
float timeDelta = 1.0f/ rb.velocity.magnitude; // for example

Vector3 position = this.transform.position;
Vector3 velocity = this.transform.position + rb.velocity + 0.5f * Physics.gravity * timeDelta * timeDelta;

for (int i = 0; i < numSteps; ++i)
{
Debug.DrawLine (position, velocity, Color.red);

position += velocity * timeDelta + 0.5f * Physics.gravity * timeDelta * timeDelta;
velocity += Physics.gravity * timeDelta;
}

}
``````

What should I do? I honestly don’t even know where to start.

Line 13: I think you want:

`````` Debug.DrawLine (position, position + velocity, Color.red);
``````

Or maybe even multiply the velocity term by Time.deltaTime otherwise your arc will be “hairy.”

Even by multiplying velocity by Time.deltaTime my arc seems to become longer the farther away I am from the center of the scene and shorter the closer I am to it.
Altough the addition of position and velocity seems to fix the weird problem I was having before.

You’re double-adding gravity, both to position and to velocity.

Line 15 should just be position += velocity * timeDelta, methinks.

oh wait, man, more errors: line 8 actually ADDS IN the position to the velocity…

Velocity should just be pure velocity. That’s why your arc is variant depending on position.

Take out the "this.transform.position + " portion of line 8

I’m fairly sure I need this.transform.position, if I remove it I get this

And if I change position += velocity * timeDelta I get this