- Home /
Why is my rigidbody jump height different every time, despite being in FixedUpdate.
void FixedUpdate()
{
Jump(jumpForce);
}
public void Jump(float jumpHeight)
{
if (isGrounded() && Input.GetButtonDown("Jump") && rB.velocity.y == 0)
{
rB.velocity = new Vector3(rB.velocity.x, 0, rB.velocity.z);
rB.AddForce(new Vector3(0, jumpHeight, 0), ForceMode.VelocityChange);
}
}
Here is my script.
For some reason the height my player character reaches is slightly different every time. Additionally, the player characters jump height is very different while in editor fullscreen. In editor he achieves a height of 6 on the y axis while not full screened. Full screen he only achieves a height of 3.
If I had to guess I'd assume it's because of the way I'm calling Jump(), but I want to make sure.
Thank you!
Answer by tdx110 · Mar 17, 2021 at 08:39 PM
I've tested your code and it's good. He always jumped at the same height. Check the number 20 instead of "jumpHeight" and check the height. Maybe it gets bad value.
Thank you, after reading your comment I realized it was because I'm using artificial gravity, which was in update. After moving to FixedUpdate it works as intended.
if (useGravity && !isGrounded())
{
rB.AddForce(Vector3.up * -9.8f * gravityMultiplier);
}
Your answer
Follow this Question
Related Questions
Why wont my character jump? (using rigidbody and addForce) 1 Answer
Character Jumping 2 Answers
Rigidbody.Addforce makes object skip 5 Answers
Questions regarding jumping 1 Answer