Skip to main content
added 274 characters in body
Source Link
David Gouveia
  • 25k
  • 5
  • 88
  • 127

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.

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.

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.

Source Link
David Gouveia
  • 25k
  • 5
  • 88
  • 127

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.