Using bools in Update function to run code once not consistent

I am trying to run this code:

void CheckIfHit()
        RaycastHit forwardHit;
        float dist;
        Vector3 dir;

        dist = 2.0f;
        dir = transform.forward;

        Debug.DrawRay(transform.position, dir * dist,;
        //if the ray forward hits something
        if (Physics.Raycast(transform.position, dir * dist, out forwardHit, dist))
            //if it hits the radiussphere
            if (forwardHit.transform.CompareTag("RadiusSphere"))
                //if not already avoiding radius loop
                if (!avoidRadiusLoop)
                    avoidRadiusLoop = true;

                    //30% chance of avoidance
                    if (Random.value < 1.0f)
                        animate.SetBool("Jump", true);
                        GetComponent<Rigidbody>().AddForce(transform.up * 15.0f, ForceMode.VelocityChange);

I want my enemy to jump over an object at a 30% (I have it at 100% now for testing) chance. This works mostly but every now and then, the Debug.Log("avoiding") line will return twice, which makes my enemy jump twice as high in the air… am i missing something or is there a better way of doing this? Thanks

There is nothing in your code that reveals how it can run twice, but then again, we can’t see the full scope of the variable avoidRadiusLoop. It appears to be a class field declared in the same script as this function? My guess is that it is being set to false unexpectedly elsewhere.

As per your comment to reply by CHPedersen,

Problem is that you’re still to close too the ground in the frame immediately after the jump, so the raycast that determinates if you’re grounded still hits.

This is the problem I had when coding my own 3D Physics in XNA framework, in C# from scratch.

Raycasting from actor’s feet downward is a horrible way to check if a player is not jumping/flying, I have no idea why everybody uses that.

Best way to do that is to check the actor’s vertical speed; in pseudo:

bool isJumping;
bool isFalling;
bool canJump;

if player’s vertical speed is positive, player is jumping, and can NOT jump;
if player’s vertical speed is negative, player is falling and NOT jumping and can NOT jump;
if player is falling and NOT jumping, AND it’s vertical speed is now below some tiny threshold = now the player is NOT falling, is grounded, and CAN jump again.

Actually, disregard my previous answer if you’re just looking to quickly fix up your code:

Simply add a 0.2 seconds (or so) of a cooldown after immediate jump.

So, if all the IFs have passed and actor jumps, use AddForce, and set a timer which disables the raycasting for checking isGrounded for some 0.2 seconds (don’t forget to also manually set isGrounded = false immediately).

Here’s your code:
I’m assuming all of this is within the same script: