My player jumps higher than he is supposed to

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody2D rb;
    public float velocity;
    public bool canIJump;
    public float jumpForce;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            leftMovement();
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            rightMovement();
        }
        if (Input.GetKey(KeyCode.UpArrow) && canIJump==true)
        {
            jump();
        }
    }
    public void leftMovement()
    {
        Vector2 speed = new Vector2(-1 * velocity * Time.deltaTime, rb.velocity.y);
        rb.velocity = speed;
    }
    public void rightMovement()
    {
        Vector2 speed = new Vector2(1 * velocity * Time.deltaTime, rb.velocity.y);
        rb.velocity = speed;
    }
    public void jump()
    {
        Vector2 jumpForceDirection = new Vector2(rb.velocity.x, jumpForce * Time.deltaTime);
        rb.velocity = jumpForceDirection;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Floor"))
        {
            canIJump = true;
        }
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Floor"))
        {
            canIJump = false;
        }
    }
}

I don’t know why but my player jumps higher than he is supposed to. Sometimes happens, and I can’t find a pattern that makes he do that.

This is most likely because you’re using Time.deltaTime in your Jump method, which isn’t necessary here since you’re setting the player’s Y velocity instantly instead of using force to move it over time.

Time.deltaTime varies per frame, which explains why your player’s jump varies depending on the frame they jump on.

Try removing Time.deltaTime to see if your bug has been resolved or not. :slight_smile:

It’s because you’re using Input.GetKey(KeyCode.UpArrow) to check for jumps. This means that you will try to jump every frame the jump button is down, instead of just when it’s pressed.

This would be fine if the OnCollisionExit with the floor happened immediately after you jumped (before the next Update). But that’s not guaranteed to happen, the physics doesn’t run in sync with the game. So you can get a couple of Update calls before the OnCollisionExit happens. If you have the default Physics Timestep of 50fps and you have vsync on, you’ll get 2 in a row before the OnCollisionExit pretty often.

There’s a couple of fixes. The most straightforwards one is to just use Input.GetKeyDown instead of Input.GetKey for the jump check. That does mean that you won’t jump automatically after landing if you land while holding down the button, I don’t know if that was something you wanted.