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;
}
}