NPC Animation
Animator
NPC Animation: Animator, Coroutine, Button

Last updated

Last updated
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//attach to an NPC that has animator controller
//adjust as necessary for
public class NPCAnimation : MonoBehaviour
{
// Start is called before the first frame update
enum NPCState { idle, attack}
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
animator.SetInteger("NPCState", (int)NPCState.idle);
StartCoroutine(NPC_Animation());
}
//looping transistions between states: idle, attack
IEnumerator NPC_Animation()
{
while (true) // you can put there some other condition
{
animator.SetInteger("NPCState", (int)NPCState.idle);
yield return new WaitForSeconds(5.0f);
animator.SetInteger("NPCState", (int)NPCState.attack);
yield return new WaitForSeconds(5.0f);
}
}
public void SetIdle()
{
StopCoroutine(NPC_Animation());
Debug.Log("StopNPC_CoroutineAnimation");
animator.SetInteger("NPCState", (int)NPCState.idle);
}
public void SetAttack()
{
StopCoroutine(NPC_Animation());
animator.SetInteger("NPCState", (int)NPCState.attack);
}
}