# Smooth vector based jump

I started working on Wolfire's mathematics tutorials. I got the jumping working well using a step by step system, where you press a button and the cube moves to the next point on the jumping curve.

Then I tried making the jumping happen during a set time period e.g the jump starts and lands within 1.5 seconds. I tried the same system I used for the step by step implementation, but it happens instantly. After some googling I found that Time.deltatime should be used, but I could not figure how. Below is my current jumping code, which makes the jump happen instantly.

`````` while (transform.position.y > 0)
{
modifiedJumperVelocity -= jumperDrag;
transform.position += new Vector3(modifiedJumperVelocity.x, modifiedJumperVelocity.y, 0);
}
``````

Where modifiedJumperVelocity is starting vector minus the jumper drag. JumperDrag is the value that is substracted from the modifiedJumperVelocity during each step of the jump. Below is an image of the jumping curve:

-

I believe the reason this is happening "instantly" is because you seem to be using a while loop inside your update loop. That would cause your characters position to go through the entire jump inside a single frame, meaning it would only be drawn at the end of the jump. Try something like this:

``````if (transform.position.y > groundHeight)
{
modifiedJumperVelocity -= jumperDrag;
transform.position += new Vector3(modifiedJumperVelocity.x, modifiedJumperVelocity.y, 0);
}
``````
-
Doh! Only paid attention to the inside and missed this mistake :) – David Gouveia Jun 19 '12 at 13:41
Easy to miss :) Now OP can just combine the answers! – Byte56 Jun 19 '12 at 13:41
Combined and it works like a charm, thank you both! I could accept only one answer, so I had to toss a coin :) – Esa Jun 20 '12 at 6:27

The answer given by Byte56 describes one fundamental mistake in your code that you must fix. But afterwards you'll still want to factor in the delta time into your movement otherwise it is likely to occur too fast and depend on the framerate of the game.

In that regard, I think you're trying to implement euler integration (although if you read that article you will see that he recommends using a more complicated method). It's usually implemented like this:

``````velocity += acceleration * deltaTime
position += velocity * deltaTime
``````

Which in your example would become the following (considering `jumperDrag` to be your acceleration):

``````modifiedJumperVelocity -= jumperDrag * deltaTime;
transform.position += new Vector3(modifiedJumperVelocity.x, modifiedJumperVelocity.y, 0) * deltaTime;
``````

Afterwards, just tweak your initial velocity and acceleration values to change the duration of the jump.

By the way I think `jumperDrag` would make more sense being called `gravity` and defined as a vector pointing donwards, so that you can get rid of the minus sign. And you might as well use `Vector3` everywhere to avoid the conversion at the end and simplify everything.

-