- Home /
Fixed jumping in platformer jump
I am making a platformer game and implemented jumping, but I have an issue,when I multiply the JUMP VALUE with Time.deltaTime , the value is slightly different every time. Is it possible to make the player jump with same value every time the player jumps? Here is my code.
void Update () {
if(gameManager.paused) return;
isPressed = false;
if (Input.GetKey(KeyCode.LeftArrow) || isBtnLeft)
{
isPressed = true;
xSpeed -= (SPEED_STEP*Time.deltaTime);
xSpeed = xSpeed<-MAX_X_SPEED ?-MAX_X_SPEED:xSpeed;
}
if (Input.GetKey(KeyCode.RightArrow) || isBtnRight)
{
isPressed = true;
xSpeed += (SPEED_STEP*Time.deltaTime);
xSpeed = xSpeed>MAX_X_SPEED ?MAX_X_SPEED:xSpeed;
}
if (Input.GetKeyDown(KeyCode.UpArrow) || isBtnJump){
if(!isJumping){
isBtnJump = false;
isJumping = true;
squish(0.5f,1.4f);
float currentJumpSpeed = (jumpSpeed*Time.deltaTime);
currentJumpSpeed = currentJumpSpeed>MAX_JUMP_SPEED ? MAX_JUMP_SPEED:currentJumpSpeed;
ySpeed += jumpSpeed;
}
}else
ySpeed -=(gravity*Time.deltaTime);
ySpeed = ySpeed<MAX_FALL_SPEED ? MAX_FALL_SPEED:ySpeed;
if (Mathf.Abs(xSpeed)<1&&! isPressed) {
xSpeed=0;
}
collisionY();
collisionX();
xSpeed *=hFriction;
}
}
What if i dont multiple the jumpSpeed with delta time?
Thanks
Answer by supernat · Mar 19, 2014 at 04:55 AM
I assume you have code that is decreasing the Y speed value every frame, accounting for gravity. If so, your jump speed should not be based on time. Let's call it an initial input velocity instead. If you jumped with an initial velocity of say 20 ft/s every time, then you would start in frame 0 of the jump with an upward velocity of 20, and each frame thereafter, the upward velocity would decrease by the acceleration over time. In other words, you would account for frame time when determining how far the player has fallen back to the ground between two frames, but the first frame would always be 20 ft up. So I think you just want to set an "initial velocity" up (no frame time concerns) and then subtract from that each frame based on the change in velocity since the last frame due to gravity, which is just the gravity (m/s)/s multiplied by deltaTime. Then move the player down a distance by the "current velocity" each frame, which again is just velocity (m/s) multiplied by deltaTime.
Thanks a lot for the reply! in short, I just have to multiply the gravity with DeltaTime right?and then subtract it from the ySpeed?
Another question, is it possible to skip update for one frame? I have a bouncy platform, when the player touches it, the player bounces up. In this case, if I apply the bounce speed without multiplying with delta time, there are chances that gravity is also being applied in that frame. So how can I skip that?
It depends. Are you doing your own gravity? If so, then yes, multiply gravity time deltaTime, then add that value to your current velocity. Then multiply velocity by deltaTime, which gives you distance moved between frames, and add that distance to your position.
It sounds like you're relying on the physics engine though, in which case the only way to skip using gravity is to turn Is$$anonymous$$inematic on for a frame for the character and off the next. Actually you could probably also force the velocity of the character to a specific value (i.e. the 20 above) in the LateUpdate() method which is called after physics calculations have occurred.
If you are using the physics engine, you don't need to control your own velocity, gravity, etc. All you need to do is add an input force to the character and let the physics do the rest. I'm not sure I follow the question about bouncing. If you were to hit a bouncy platform, would the player really detect a single frame difference due to gravity? I mean your height is dependent on the position of the platform when the bounce starts to begin with. If you're using the physics engine bounce, I don't know, I don't really find it very consistent. I like to detect a collision with the platform and add my own upward force so that it consistently moves the same amount in a bounce.
I am doing my own physics, I am adding my own upward force like you just said, but that upward force is added when the player collides with the bounce platform not the update function. Due to that I don't know in which line the Update function will be executing when the collision happens.
What if I move everything to fixedUpdate? Will that fix the issue? and any side effects of that?
FixedUpdate is a good place to handle physics generally speaking, so I would put everything related to forces in there. You still have to multiply your rates by the fixed time, but what this means is that they become constants. I still prefer to multiply my rates by the fixedDeltaTime value, because then you can adjust the physics time step size without changing your code. Some will suggest that you can just add velocities without concern for time (even Unity suggests this in the reference material), but to me that doesn't make sense because it breaks when you adjust the time steps, because then you have to adjust the constants you add in FixedUpdate too. I think it's always best to go with a fixed frame time though when you can, so yes.
I may be missing something. You said the upward force is added when you collide with the platform but not the update function. I don't think you would ever add forces from update, unless it was based on a flag or a constant force. But you mean in your OnTrigger or OnCollision method, you are adding the force due to the bound (please explain exactly how that related to the posted code, or update your post with that code), and in your update method (I assume above) you are adding force based on pressing of a key. Are both places updating the yspeed variable? Are you setting isJumping to true when you hit the bouncy platform?
I have updated the code in first post, and yes i am updating ySpeed based on keypress. Here is Trigger Function of the bouncy platform.
public void OnTriggerEnter(Collider collider){
if(collider.gameObject.name.Equals("playerCollider")){
player.onJumpPlatform(JU$$anonymous$$P*Time.deltaTime);
}
}
// in player class public void onJumpPlatform(float jumpValue){ isJumping = true; ySpeed = jumpValue; }
So multiplying the gravity and jump speed with fixedDeltaTime wont really have an effect on the game? In platformer games, why do some people multiply values with deltaTime? Shouldn't it always be fixed?Because I always want the jump to be of the same height.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player color change on collision -1 Answers
Staring a 2D game 2 Answers
Change gravity on button 1 Answer
move, destroy self, instantiate again, repeat. 2 Answers
2D - Detect Slopes x Walls 1 Answer