OnCollision Jump Occurring Twice

Hi guys, I have a problem with my spherical player jumping more times than it should if it’s near two ground colliders, I’ve been able to fix this problem before using rays, but I’ve recently changed it so I get the normals of the object collision, there-for I need OnCollision for this to work correctly.

So I need to be able to only jump once even if touching two colliders.

void OnCollisionStay(Collision theCollision)
    {
        if (theCollision.gameObject.tag == "Ground")
        {
            onGround = true;
            canJump = true;
            Vector3 normalDirection = theCollision.contacts[0].normal;

            if (Input.GetButton("Jump") && canJump)
            {
                GetComponent<Rigidbody>().AddForce(normalDirection * jumpDistY);
                canJump = false;
            }
        }
    }

    void OnCollisionExit(Collision theCollision)
    {
        if (theCollision.gameObject.tag == "Ground")
        {
            onGround = false;
        }
    }

If you might know the answer to this, please let me know, I’d very much appreciate the help! Thank you!

From Unity docs:

OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.

If your player is colliding with two colliders, the OnCollisionStay function will be called twice. On each of these calls you set canJump to true, then use it in a condition. The result is that canJump will always be true when it’s checked in the jump condition.

The solution is to call AddForce in the FixedUpdate (or Update) function. Use OnCollisionStay only to set canJump to true and OnCollisionExit to set it to false. Check canJump in the Update function and AddForce will fire only once.