On idle animation play doesnt start

Hi, i am working on a personal unity project but i am stuck on my wandering AI, i want the pinguin to go idle after a couple of seconds and then go in the idle animation, but right now it doesn`t go into idle except on start but then it just plays the animation again every frame so it is very buggy. how can i let the pinguin wander with a walking animation and go in idle after a couple of seconds and play the idle animation. I hope one of you can help me with this problem.

my Code:

using UnityEngine;
using System.Collections;
using UnityEngine.AI;


public class PinguinAI : MonoBehaviour
{

    public float Radius;
    public float Timer;

    private Transform target;
    private NavMeshAgent agent;
    private float currentTimer;

    private bool idle;
    public float idleTimer;
    private float currentIdleTimer;

    private Animator anim;

    void OnEnable()
    {
        anim = GetComponent<Animator>();
        agent = GetComponent<NavMeshAgent>();

        currentTimer = Timer;
        currentIdleTimer = idleTimer;
    }


    void Update()
    {
        currentTimer += Time.deltaTime;
        currentIdleTimer += Time.deltaTime;

        if (currentIdleTimer >= idleTimer)
        {
            StartCoroutine("switchIdle");
        }

        if (currentTimer >= Timer && !idle)
        {
            Vector3 newPos = RandomNavSphere(transform.position, Radius, -1);
            agent.SetDestination(newPos);
            currentTimer = 0;
        }

        if (idle == false)
        {
            anim.SetBool("Walking", true);
        }
        else
        {
            anim.SetBool("Idle", true);
        }
    }

    IEnumerator switchIdle()
    {
        idle = true;
        yield return new WaitForSeconds(5);
        currentIdleTimer = 0;
        idle = false;
    }

    public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
    {
        Vector3 randDirection = Random.insideUnitSphere * dist;

        randDirection += origin;

        NavMeshHit navHit;

        NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);

        return navHit.position;
    }
}

You need to set both “Walking” and “Idle” to false at some point like so:

if (idle == false)
         {
             anim.SetBool("Walking", true);
             anim.SetBool("Idle", false);
         }
         else
         {
             anim.SetBool("Walking", false);
             anim.SetBool("Idle", true);
         }

BTW using transitions from the “Any State” state could cause the issue here the animation play it self again an again, because the transition is happening to the walk state to the walk state. To fix that only use transition between your actual animation states.

Edit: also having two valid transition’s (meaning with all it’s conditions fulfilled) could cause a ping pong between two or more states. To debug that just go to your animator window and select the Pinguin while in play mode.

@NielsNL4 In your case the animator forces to play the animation because the boolean walking stays at true.
You should not use boolean in your animator. Use trigger instead.
A trigger is like a boolean. But it switches to false when used.

void Update()
     {
         currentTimer += Time.deltaTime;
         currentIdleTimer += Time.deltaTime;
 
         if (currentIdleTimer >= idleTimer)
         {
             StartCoroutine("switchIdle");
         }
 
         if (currentTimer >= Timer && !idle)
         {
             anim.SetTrigger("Walking");
             Vector3 newPos = RandomNavSphere(transform.position, Radius, -1);
             agent.SetDestination(newPos);
             currentTimer = 0;
         }
     }
 
     IEnumerator switchIdle()
     {
         idle = true;
         anim.SetTrigger("Idle");
         yield return new WaitForSeconds(5);
         currentIdleTimer = 0;
         idle = false;
     }