Double Jump turns into Triple Jump sometimes

I’m using a counter to implement double jump, but it only works sometimes. The character is able to triple jump from time to time.

public class PlayerMovement : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform grounded;
    public LayerMask playerMask;
    public Rigidbody rb;
    bool isGrounded;
    float distToGround;
    int counter;
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {
        if(Physics.OverlapSphere(grounded.position, 0.2f, playerMask).Length > 0)
        {
            isGrounded = true;
            counter = 0;
        }
        else
        {
            isGrounded = false;
        }

        if(Input.GetButtonDown("Jump") && counter < 2)
        {
            counter++;
            rb.AddForce(0, 1000, 0);
        }
    }

    private void FixedUpdate()
    {
        if (!isGrounded)
        {
            rb.AddForce(0, -50, 0);
        }
    }
}

6678829--765382--upload_2021-1-1_22-12-1.png
The Player Mask is set to everything except for the character itself.
Thank you very much for your time and help!

If you think the test in line 18 is hitting something that it should not, revamp the test to return the RaycastHit objects and print their names so you can see what it is you hit.

Yes, an errant hit is probably the cause. Also you can print out the counter values and see, it will probably get reset to 0 unexpectedly.

Is there any thing common among all the time you can triple jump?
unless you have a bug at the test @Kurt-Dekker mentioned you could also have some colliders extending beyond the renderers.
if you just walk on a perfect flat plane does it still happen?
does the delay between each jump matter? if you wait a second before trying to jump again in the air can you still do it or do you gotta do it in succession with a previous jump?

When triple jump happens, the counter remains at 0 after the first space hit.

Thanks, I will try that!

I can’t seem to find the pattern of triple jump, I tried to remove all other game objects except for the flat plane and the character, triple jump still happens.

Problem solved. I think it was the problem of FixedUpdate() and Update(). I tried to move the ground check and jump code to FixedUpdate(), and Update() only checks for if the space button is pressed. So I initialized a bool variable jumped in Updated(), whenever space is pressed, jumped is set to true. And in FixedUpdate(), before applying force to the character, check if jumped is true. I am new to Unity and I’m not sure why this happens, I’m guessing it’s because Update() and FixedUpdate() run on different speed, so they need to have an extra condition to align them.
Thanks for all the helps!