W
W
WasTabon2020-10-04 17:55:09
Unity
WasTabon, 2020-10-04 17:55:09

Why doesn't the character jump right away?

There are times when the character jumps only after a few milliseconds, mostly when I poke the spacebar several times in a row

public float speed;
    public float jumpForce;

    public bool isGrounded;

    public Transform transform;
    public Rigidbody2D rigidbody2D;

    void FixedUpdate()
    {
        if (Input.GetKey("d"))
        {
            transform.Translate(Vector2.right * Time.deltaTime * speed);
        }

        if (Input.GetKey("a"))
        {
            transform.Translate(Vector2.left * Time.deltaTime * speed);
        }

        if (Input.GetKeyDown("space") && isGrounded)
        {
            rigidbody2D.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanielMcRon, 2020-10-04
@WasTabon

Physics is called in FixedUpdate. Input and stuff in Update. And you get it like this, you press the button, and during this time 2-3 frames skip and this is the delay

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question