Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity. Fixed Update isn't sensing my jump input well. Normal update launches my character to random heights when space is pressed

I don't know what's up. I usually do my jumping on the update function. This is my first big game though other than other prototypes or bad game attempts I've done.

This is my jump code

void FixedUpdate()
{
    if (isDead == false)
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (OnGround == true)
            {
                rb.velocity=  new Vector2 (rb.velocity.x, jumpforce * Time.deltaTime);
            }
        }
    } 
}

I sometimes miss a few jumps on fixed update. But when I put it in my update function it jumps so low sometimes, like two or three times and then it launches me so high. It's just so random and weird. Is there anything I can do about this or will I just have to deal with fixedupdate? I even tried lateupdate if that matters.

like image 527
John Avatar asked Oct 16 '25 02:10

John


1 Answers

The general rule of thumb is to place all keyboard/mouse input into the Update method and not the FixedUpdate method. The reason for this is because FixedUpdate is not a frame by frame process, but is called at fixed time increments which can be more or less calls in a certain frame period.

Input data is determined every frame, so in FixedUpdate where it can run in-between frames, skip frames, etc. you have the possibility of losing input. Instead, move your input code to the Update method which is called every frame. You will no longer lose input.

The issue with setting your physics calls in Update is because all physics code should be placed in FixedUpdate. As Update can run faster or slower than the physics system in your game, placing physics-based calls in it can have unexpected results.

Unity:

FixedUpdate should be used instead of Update when dealing with Rigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update. Tell me more...

The solution is to cache your input from Update and then set using the resulting input from this method in FixedUpdate to determine your physics.

private bool justJumped = false;

void FixedUpdate()
{
    if(justJumped)
    {
        justJumped = false;
        rb.AddForce(Vector2.up * jumpForce);
    }
}

void Update()
{
    if (!justJumped && Input.GetKeyDown(KeyCode.Space) && OnGround && !isDead)
    {
        justJumped = true;
    }
}

I would also recommend not setting velocity directly as if any other outside forces are acting on your player, they will be disregarded. Instead, use AddForce. I swapped your direct velocity set with an AddForce. I would also not multiply by Time.deltaTime. That value is just a float value for how much time has passed since the last frame. It should be used when doing movement in small increments such as with a Lerp. If you want to set velocity directly, remove the Time.deltaTime product and decrease your jumpforce by a lot. You will find the outcome to be the same.

like image 175
TEEBQNE Avatar answered Oct 18 '25 21:10

TEEBQNE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!