My NPC is not switching from running away state to random movement state.

Here’s the code I wrote. The NPC does random movement with the RandomMovement() method. When the player gets behind the NPC and is detected by the Physics.SphereCast, the seenplayer bool gets set to true and the Runaway() method should run. The issue I’m having is that when the player is chasing the NPC and the player stops, the NPC should continue running away up to the runawayRadius distance which is further away that the spherecast rearViewDectionRange.

But the NPC just runs away to a up to a random distance and just stays there, it doesn’t switch back to the RandomMovement() method . Unless I walk backwards away from the NPC it somehow starts the RandomMovement() method again.

void FixedUpdate ()
{
    Debug.Log(distance);
    NavMeshAgent agent = GetComponent<NavMeshAgent>();
    
    RaycastHit rHit;

    if (!seenPlayer)
    {
        RandomMovement();

        if (Physics.SphereCast(eyes.transform.position, sphereCastThickness, -transform.forward, out rHit, rearViewDetectionRange))
        {
            if (rHit.collider.tag == "Player")
            {
                seenPlayer = true;
            }
        }
        else
            seenPlayer = false;
    }

    if (seenPlayer)
    {
        Runaway();
    }
}

void RandomMovement ()
{
    seenPlayer = false;
    runningAway = false;
    
    distance = Vector3.Distance(player.position, transform.position);
    Vector3 randomDirection = Random.insideUnitSphere * walkRadius;
    randomDirection += transform.position;
    NavMeshHit hit;
    NavMesh.SamplePosition(randomDirection, out hit, walkRadius, 1);
    Vector3 finalPosition = hit.position;
    NavMeshAgent agent = GetComponent<NavMeshAgent>();
    if (!agent.pathPending)
    {
        if (agent.remainingDistance <= agent.stoppingDistance)
        {
            if (agent.hasPath || agent.velocity.sqrMagnitude == 0f)
            {
                if (timerStart == false)
                {
                    agent.speed = walkSpeed;
                    Debug.Log("Destination arrived.");
                    idleTime = Random.Range(0, 11);
                    timerStart = true;
                    agent.autoBraking = true;
                }

                if (timerStart == true)
                {
                    idleTime -= Time.deltaTime;
                }
                if (idleTime <= 0f)
                {
                    timerStart = false;
                    Debug.Log("Agent Idle complete.");
                    agent.CalculatePath(finalPosition, path);
                    agent.SetPath(path);
                }
            }
        }
    }
}
        

void Runaway ()
{
    if (seenPlayer)
    {
        runningAway = true;
        if (runningAway)
        {
            NavMeshAgent agent = GetComponent<NavMeshAgent>();
            distance = Vector3.Distance(player.position, transform.position);
            if (runawayRadius > distance)
            {
                timerStart = false;
                //agent.SetDestination(newPos);
                //agent.SetDestination(newPos);
                StartCoroutine(PathCalls());
            }
            if (runawayRadius <= distance)
            {
                runningAway = false;
                agent.speed = walkSpeed;
                RandomMovement();
            }
        }
    }
}

Enumerator PathCalls ()
{
    NavMeshAgent agent = GetComponent<NavMeshAgent>();
    agent.speed = runAwaySpeed;
    dirToPlayer = transform.position - player.transform.position;
    newPos = transform.position + dirToPlayer;
    agent.CalculatePath(newPos, path);
    agent.SetPath(path);
    agent.autoBraking = false;
    yield return new WaitForSeconds(timeBetweenPathCalls);
}

Okay it seems to be a problem with IEnumerator PathCalls(). I have a debug log there that shows that its being called but the code there isn’t executing. I think theres a problem with my path finding code.