Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

enter image description here

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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);
 }
share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.