Double jump not working anymore after implementing a crouching mechanic

Hey, so im making a endless runner with a double jump and crouch mechanic, the double jump worked fine before i added the crouching mechanic, so the problem that’s causing the bugg is most likely inside the crouch mechanic but i can’t find it. i am pretty new to programming and i just can’t figure it out. if anyone knows whats wrong with my code let me know. this is my script:

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
using UnityEngine.Events;

public class PlayerMovement : MonoBehaviour
{  
    [SerializeField] public Transform m_CeilingCheck;                          // A position marking where to check for ceilings
    [SerializeField] public Collider2D m_CrouchDisableCollider;                // A collider that will be disabled when crouching
  
    const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up

    public float speed;
    public float jumpforce;
    private bool isgrounded;
    public Transform groundcheck;
    public float checkradius;
    public LayerMask whatisground;
    private int extrajumps;
    public int extrajumpsvalue;
    private bool crouch = false;



    private Rigidbody2D rb;

    void Start()
    {      
        extrajumps = extrajumpsvalue;
        rb = GetComponent<Rigidbody2D>();
    }


    void FixedUpdate()
    {
        //Checks if player is grounded
            isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisground);
        //Moves our character
            rb.velocity = new Vector2(speed, rb.velocity.y);


               
            // If crouching
            if (Input.GetKey(KeyCode.S) && crouch == false)
            {
            //Changing the mass so the player falls faster
                    rb.mass = 1.5f;
            UnityEngine.Debug.Log("Mass:" + rb.mass);
            //Set crouching to true
                    crouch = true;                                       
            // Disable one of the colliders when crouching
                    m_CrouchDisableCollider.enabled = false;
                  

            }
            else if(!Input.GetKey(KeyCode.S))
            {
                //Checks if player can stand up
                if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, whatisground))
                {

                //Keep mass the same, still crouching
                    rb.mass = 1.5f;

                //Set crouching to true
                    crouch = true;
                }
                else
                {
                //Changing the mass so the player falls at the normal speed;
                    rb.mass = 1.0f;
                //Set crouching to true
                crouch = false;
                //enable one of the colliders when not crouching
                    m_CrouchDisableCollider.enabled = true;
                }
            }

      




    }

    void Update()
    {

        //Jump mechanics
        if (isgrounded == true)
        {
          
            extrajumps = extrajumpsvalue;
        }

        if (Input.GetKeyDown(KeyCode.Space) && extrajumps > 0)
        {
          
            rb.velocity = Vector2.up * jumpforce;
            extrajumps--;
        }
        else if (Input.GetKeyDown(KeyCode.Space) && extrajumps == 0 && isgrounded == true)
        {
          
            rb.velocity = Vector2.up * jumpforce;
        }
    }

}

Not 100% sure what the immediate issue is but I see several large issues with the code:

  • Reading Input in FixedUpdate. FixedUpdate dies not necessarily run every frame, or it might run multiple times per frame. You’re liable to either miss input or count it multiple times when you read input in FixedUpdate. Best practice is to read the input in Update () and apply its results in FixedUpdate.
  • RigidBody operations in Update (). Again not a great idea because RigidBody operations are designed to run in a fixed rate function like FixedUpdate. You open yourself up to missing jumps etc if there is no corresponding physics update for a given frame when the user presses the jump button for example.
  • In what world does changing an objects mass change the rate at which it falls due to gravity?
1 Like

Thanks for ur reply it helped a lot, there are still a few problems tho, double jumping is still not working and now if i just jump once the character goes to the left a bit? like when i press space there is some kind of force pushing it to the left and then it goes up. this is my code now, i think its a little bit better but if you know what i still can improve and maybe know why jumping is so weird right know i would love your help.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
using UnityEngine.Events;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] public Transform m_CeilingCheck;                          // A position marking where to check for ceilings
    [SerializeField] public Collider2D m_CrouchDisableCollider;                // A collider that will be disabled when crouching

    const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up

    public float speed;
    public float jumpforce;
    private bool isgrounded;
    public Transform groundcheck;
    public float checkradius;
    public LayerMask whatisground;
    private int extrajumps;
    public int extrajumpsvalue = 1;
    private bool crouch = false;

    private bool doJump;
    private bool doJump1;

    private bool doCrouch;
    private bool doCrouch1;


    private Rigidbody2D rb;

    void Start()
    {
        doCrouch = false;     

        doJump = false;
        doJump1 = false;

        extrajumps = extrajumpsvalue;
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        //Checks if player is grounded
        isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisground);

        //Jump inputs
        if (isgrounded == true)
        {
            extrajumps = extrajumpsvalue;
        }

        if (Input.GetKeyDown(KeyCode.Space) && extrajumps > 0 && !doJump)
        {
            doJump = true;

        }
        else if (Input.GetKeyDown(KeyCode.Space) && extrajumps == 0 && isgrounded == true && !doJump1)
        {
            doJump1 = true;

        }

        // If crouching
        if (Input.GetKey(KeyCode.S) && crouch == false)
        {
            doCrouch = true;

        }
        else if (!Input.GetKey(KeyCode.S))
        {
            //Checks if player can stand up
            if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, whatisground))
            {
                doCrouch = true;             
            }
            else
            {
                doCrouch = false;
            }
        }




    }
    void FixedUpdate()
    {

        //Moves our character
        rb.velocity = new Vector2(speed, rb.velocity.y);

        if (doCrouch == true)
        {
            //Changing the mass so the player falls faster
            rb.mass = 1.7f;
            //Set crouching to true
            crouch = true;
            // Disable one of the colliders when crouching
            m_CrouchDisableCollider.enabled = false;
        }
        else if (doCrouch == false)
        {               
             //Changing the mass so the player falls at the normal speed;
             rb.mass = 1.0f;
             //Set crouching to true
             crouch = false;
             //enable one of the colliders when not crouching
             m_CrouchDisableCollider.enabled = true;
         
        }

        //Jump Mechanics
        if (doJump == true)
        {
            UnityEngine.Debug.Log("A");
            rb.velocity = Vector2.up * jumpforce;
            doJump = false;
            extrajumps--;
        }

        if (doJump1 == true)
        {
            //UnityEngine.Debug.Log("AA");
            rb.velocity = Vector2.up * jumpforce;
            doJump1 = false;
        }

    }
}

So i found the problem in my code and fixed it. here’s the code if anyone wants it :slight_smile:

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
using UnityEngine.Events;

public class PlayerMovement : MonoBehaviour
{
    public Transform m_CeilingCheck;                          // A position marking where to check for ceilings
    public Collider2D m_CrouchDisableCollider;                // A collider that will be disabled when crouching

    const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up

    public float speed;
    public float jumpforce;
    private bool isgrounded;
    public Transform groundcheck;
    public float checkradius;
    public LayerMask whatisground;
    private int extrajumps;
    public int extrajumpsvalue = 1;
    private bool crouch = false;


    private bool doCrouch;



    private Rigidbody2D rb;

    void Start()
    {
        doCrouch = false;       


        extrajumps = extrajumpsvalue;
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        //Checks if player is grounded
        isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisground);

        //Jump mechanics
        if (isgrounded)
        {
            extrajumps = extrajumpsvalue;
        }

        if (Input.GetKeyDown(KeyCode.Space) && isgrounded)
        {

            rb.velocity = Vector2.up * jumpforce;
           
        }
        else if (Input.GetKeyDown(KeyCode.Space) && extrajumps > 0 && !isgrounded)
        {
            rb.velocity = Vector2.up * jumpforce;
            extrajumps--;
        }


        // If crouching
        if (Input.GetKey(KeyCode.S) && crouch == false)
        {
            doCrouch = true;

        }
        else if (!Input.GetKey(KeyCode.S))
        {
            //Checks if player can stand up
            if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, whatisground))
            {
                doCrouch = true;               
            }
            else
            {
                doCrouch = false;
            }
        }




    }
    void FixedUpdate()
    {

        //Moves our character
        rb.velocity = new Vector2(speed, rb.velocity.y);

        if (doCrouch)
        {
            //Changing the mass so the player falls faster
            rb.mass = 1.7f;
            //Set crouching to true
            crouch = true;
            // Disable one of the colliders when crouching
            m_CrouchDisableCollider.enabled = false;
        }
        else if (doCrouch == false)
        {                 
             //Changing the mass so the player falls at the normal speed;
             rb.mass = 1.0f;
             //Set crouching to false
             crouch = false;
             //enable one of the colliders when not crouching
             m_CrouchDisableCollider.enabled = true;
           
        }

       

    }
}

Changing the mass won’t make the body fall faster as mass has no effect on gravity. You can change the Rigidbody2D.gravityScale however. Also, you’re doing checks per-frame but unless you’re running physics per-frame, this is a waste as you might get several of them before the physics even runs and uses your results.

1 Like

Whoopss, i forgot to update my script i made some last changes and i forgot to update it here, i noticed that the mass change didn’t do anything so i replaced it with changing the .gravityscale :confused: Anyways here’s my updated code that works :slight_smile: i also changed the isgrounded to a property so that it always works.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
using UnityEngine.Events;

public class PlayerMovement : MonoBehaviour
{
    public Transform m_CeilingCheck;                          // A position marking where to check for ceilings
    public Collider2D m_CrouchDisableCollider;                // A collider that will be disabled when crouching

    const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up

    public float speed;
    public float jumpforce;
   
    public Transform groundcheck;
    public float checkradius;
    public LayerMask whatisground;
    private int extrajumps;
    public int extrajumpsvalue = 1;
    private bool crouch = false;


    private bool doCrouch;

    public bool isgrounded
    {
        get
        {
            return Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisground);
        }
    }

    private Rigidbody2D rb;

    void Start()
    {
        doCrouch = false;       


        extrajumps = extrajumpsvalue;
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        //Checks if player is grounded
        //isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisground);

        //Jump mechanics
        if (isgrounded)
        {
            extrajumps = extrajumpsvalue;
        }

        if (Input.GetKeyDown(KeyCode.Space) && isgrounded)
        {

            rb.velocity = Vector2.up * jumpforce;
           
        }
        else if (Input.GetKeyDown(KeyCode.Space) && extrajumps > 0 && !isgrounded)
        {
            rb.velocity = Vector2.up * jumpforce;
            extrajumps--;
        }


        // If crouching
        if (Input.GetKey(KeyCode.S) && crouch == false)
        {
            doCrouch = true;

        }
        else if (!Input.GetKey(KeyCode.S))
        {
            //Checks if player can stand up
            if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, whatisground))
            {
                doCrouch = true;               
            }
            else
            {
                doCrouch = false;
            }
        }




    }
    void FixedUpdate()
    {

        //Moves our character
        rb.velocity = new Vector2(speed, rb.velocity.y);

        if (doCrouch)
        {
            //Changing the gravity so the player falls faster
            rb.gravityScale = 5f;
            //Set crouching to true
            crouch = true;
            // Disable one of the colliders when crouching
            m_CrouchDisableCollider.enabled = false;
        }
        else if (doCrouch == false)
        {                 
             //Changing the gravity so the player falls at the normal speed;
             rb.gravityScale = 3f;
             //Set crouching to false
             crouch = false;
             //enable one of the colliders when not crouching
             m_CrouchDisableCollider.enabled = true;
           
        }

       

    }
}