- Home /
Achieving consistent jump height regardless of vertical velocity
I keep my player object (Rounded cube, like dice) in steady horizontal motion by directly changing the velocity.
cubeRB.velocity = new Vector3(maxVelocity, cubeRB.velocity.y, 0);
Vertical velocity is unconstrained, so as it moves along the ground, it bounces and spins without slowing down.
The user can press space to add a positive force to the y axis in order to jump. The problem is that since my cube is bouncing, its vertical velocity at the time of the jump is virtually always going to be negative or positive, which creates a great deal of inconsistency in jump height.
I imagine to remedy this, I'll need to do some kind of math with the current vertical velocity and how high I want the cube to jump, but I don't math very well... Any ideas or a solution would be greatly appreciated.
Jump code:
cubeRB.AddForce(0, jumpForce, 0, ForceMode.Impulse);
Answer by elenzil · Jun 16, 2017 at 06:24 PM
if you want every jump to max out at the same height, what do you want want to happen when the player jumps while already in a jump up ?
if you really want every jump to max out at the same height, then it seems like you'd want to:
if the cube is already going up, jumping should have no effect.
if the cube is going down, jumping should set the vertical velocity to the negative of the current vertical velocity.
The player can't jump again mid-jump, I've already coded for that. But it can jump while bouncing off the ground naturally.
Anyways, I wasn't getting an answer and couldn't figure it out, so I'm taking an alternative approach to the movement mechanics.
Answer by Ewejose123 · Jul 10, 2017 at 08:54 AM
Hello, i just started unity less than a week but i think i know what do you mean and how to get it. In order to get a more consisten jump when you add a force, you need to change the Y velocity to 0 first, then apply the force and it will always be the same.
cubeRb.velocity = new vector3 (cubeRB.velocity.x,0,0);
cubeRB.AddForce(0, jumpForce, 0);
This worked for me. Thanks!
I was adding a force to a rigidbody to make it jump. The idea was to implement a second jump while the body was in the air.
But it depended a lot on the vertical velocity of the body.
If the second force was added while the body had a positive or high vertical velocity the body jumped too much.
If the second force was added while the body was falling (with negative vertical velocity) the second jump was almost imperceptible.
This solution stops the body and makes it jump again as it were on the floor.
Your answer
Follow this Question
Related Questions
2D character can't jump off of platform floating in water 0 Answers
How can you instantiate a prefab with an initial burst of speed using ForceMode2D.Impulse? 2 Answers
Setting a delay before dashing. 1 Answer
How to calculate GameObject's velocity from AddForce. 0 Answers
Problem with making an object bounce. 2 Answers