Sometimes my player jumps very high.

I did this basic movement script and sometimes my player jumps very high, for no reason I can find. A normal jump, go around 1.2 up on the y axis, while the higher jump goes up 7 on the y.

My player is being organized by one parent game object with the rigidbody2d and player script, 2 children gameobject with sprite and collision detection respectively

My player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    //Player movement script
    float xInput;

    bool jumpInput;

    Rigidbody2D rg;

    [Header("Variables Editor")]
    [SerializeField] internal int jumpMax, jumpCount;

    [SerializeField] private float forceJump, walkForce;

    void GetInput()
    {
        xInput = Input.GetAxisRaw("Horizontal");
        jumpInput = Input.GetKeyDown(KeyCode.Space);
    }

    void Move()
    {
        rg.velocity = new Vector2(xInput * walkForce * Time.deltaTime, rg.velocity.y);
    }

    void Jump()
    {
       
            jumpCount++;
            if (jumpCount < jumpMax)
            {
                //jump
                Debug.Log("Pulo");
                rg.AddForce(Vector2.up * forceJump * Time.deltaTime, ForceMode2D.Impulse);
            }
            else
            {
                jumpCount--;
                return;
                //dont jump
            }
    }

    private void Awake()
    {
        rg = GetComponent<Rigidbody2D>();
    }


    private void Update()
    {
        GetInput();
    }

    private void FixedUpdate()
    {
        if (jumpInput)
        {
            Jump();
        }
        Move();
    }

}

In line 35, you are multiplying by Time.deltaTime and you do not need to. You only use Time.deltaTime when moving by distances. If you are setting velocity or adding a force, you do not need it. Because the frame rate is varying, you are adding a different force each frame. Frames that take longer will have a higher deltaTime and so will jump further. Frames that are quick will only add a small force.

OK, I’ve taken another look at your code and found another problem. Just remember that you might get more than 1 Update for each FixedUpdate call. Update runs as soon as it can but Physics runs at a default rate of 20 times per second. Your Update loop might easily be several times that.

Your current code is setting jumpInput to true or false every frame so this is happening:

  • Update runs, space pressed, jumpInput set to true
  • Update runs, space not pressed, jumpInput set to false
  • FixedUpdate runs, checks jumpInput and finds it’s false so doesn’t do a jump

Clearly this is not what you wanted. You need to look at the following code (very simplified) to see how to refactor your code.

`

using UnityEngine;
    
    public class Jumper : MonoBehaviour
    {
        [SerializeField] float forceJump = 20;
        bool jumpInput;
        Rigidbody2D rb;
    
        private void Update()
        {
            rb = GetComponent<Rigidbody2D>();
            if (Input.GetKeyDown(KeyCode.Space))
            {
                jumpInput = true;
            }
        }
    
        private void FixedUpdate()
        {
            if (jumpInput)
            {
                rb.AddForce(Vector2.up * forceJump, ForceMode2D.Impulse);
                jumpInput = false;
            }
        }
    }

You see that I only set jumpInput when the space key is pressed. I never set it to false until I have done the jump. So, it might be true for several Update loops but that’s OK. It’s a request to do a jump next time the Physics engine is ready to do so.

You are checking Input in Update()
and checking Jump() in FixedUpdate()

their calling rates are different, so I guess, while your
jumpInput == true,
Jump() could be called TWICE. so it looked like Super Jump

move Jump to Update or,
set false to JumpInput after you call jump