4

I am trying to jump with rigidbody, and sometimes my jump is low like it stacks in something and sometimes is high. Why does it change?

thank's for the answers

My code:

private void Update() {

    if (Input.GetKeyDown(KeyCode.Space) && isGrounded) Jump();
}

private void FixedUpdate()
{
    isGrounded = Physics.CheckSphere(groundCheck.position, 0.2f, groundMask);
}

private void Jump()
    {
        rigidbody.AddForce(Vector3.up * 20, ForceMode.VelocityChange);
        isGrounded = false;
    }
  • How is your ground layer collider setup? Is your game 2D or 3D? How terrain/ground setup? Please give more info. – SeLeCtRa Feb 11 '21 at 11:25
  • Sorry I didn't realize but still your question missing ground setup? Maybe your code isn't the problem but editor setup is the problem. – SeLeCtRa Feb 11 '21 at 12:02
  • Show us inspector and also, I don't know, but you may need to compute is grounded in your update function as well, or processing input in the fixedUpdate function. Why separate? – Adrien G. Feb 11 '21 at 12:16

1 Answers1

0

First of all, any Physics calculations regarding Rigidbody (such as the one in your Jump()) should always be done inside the FixedUpdate() function according to the Unity documentation here. FixedUpdate() runs at a default fixed rate every 0.02 seconds, whereas Update() runs once per frame, which would be different for every machine it runs on.

Instead of me reinventing the wheel for you, I done a search and found the following code on the Unity forums, here, which should help you understand your problem further. I used the code there and edited it for your scenario.

private bool shouldJump = false;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    {
       shouldJump = true;
    }
}

void FixedUpdate()
{
    // Check for jump
    if (isGrounded && shouldJump)
    {
        shouldJump = false;

        rigidbody.AddForce(Vector3.up * 20.0f, ForceMode.VelocityChange);
    }
}
RStevoUK
  • 436
  • 4
  • 10
  • take care, it is naive to think that physics calculations "must" be done in FixedUpdate. you certainly can /often should but do not have to and in some cases you don't (obviously you use the frame time, **in either case**, for numerically precise calculations) example discussion under this answer: https://stackoverflow.com/a/65814658/294884 – Fattie Feb 11 '21 at 13:33
  • note that multipying by the "height" is meaningless physically. (of course, you can multiply by any number, say "3.342f" and it will do "something".) Game programmers need to fully understand Newtonian physics and use actual forces. (Unity offers four modes that do different Newtonian calculations for you, but, in all events you need to understand simple middle-school Newtonian physics to use it meaningfully.) – Fattie Feb 11 '21 at 13:36
  • Regarding the comment about multiplying by height is meaningless. I was supposed to change it to match the original post. You multiply a vector by the desired thrust, represented as a float. As demonstrated in the Unity documentation. https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html. – RStevoUK Feb 11 '21 at 13:46
  • FYI I don't know what "thrust" means technically in a newtonian physics sense. (Is it Newtons? Time derivative of Newtons?) There are four "modes" to choose from when you use AddForce. (As is usual with Unity, they are extremely badly documented and poorly thought-out.) There is some rough documentation for the four at https://docs.unity3d.com/ScriptReference/ForceMode.html . (Note that in the example code, it has "VelocityChange", mode four.) It is unlikely (but not impossible) you want to be using VelocityChange mode when it's being called every frame. THere are ... – Fattie Feb 11 '21 at 14:36
  • .. many long discussions on the www about the four different modes, which help explain the math for those who've forgotten newtonian physics from high school – Fattie Feb 11 '21 at 14:37
  • If you need to learn then take a look. NASA describes Thrust on their website: https://www.grc.nasa.gov/www/k-12/airplane/thrsteq.html - I do agree with the VelocityMode point though. I generally use VelocityMode.Impulse for a jumping motion but if you only apply VelocityMode.VelocityChange once via a bool switch it should act in a similar way as an Impulse. – RStevoUK Feb 11 '21 at 14:52
  • note that "thrust" as used in engine design (ie, achieving force through acceleration of a gas/fluid), is calculated using the mass flow rate equations (see that page). "thrust" has no connection to the PhysX system in Unity, which deals with Force (ie, Newtons, or LBF in pre-IS units), as in F = ma. using "thrust" instead of the term "force" would be sort of like using "horsepower" when one means "speed". {PhysX also deals with Torque} – Fattie Feb 11 '21 at 15:11