using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Animations : MonoBehaviour
{
public enum AnimatorStates
{
WALK, RUN, IDLE
}
private Animator _anim;
void Awake()
{
_anim = GetComponent<Animator>();
}
public void PlayState(AnimatorStates state)
{
string animName = string.Empty;
switch (state)
{
case AnimatorStates.WALK:
animName = "Walk";
break;
case AnimatorStates.RUN:
animName = "Run";
break;
}
if (_anim == null)
_anim = GetComponent<Animator>();
_anim.Play(animName);
}
void Update()
{
if (MyCommands.walkbetweenwaypoints == true)
{
PlayState(AnimatorStates.RUN);
}
else
{
PlayState(AnimatorStates.IDLE);
}
}
}
The code is working fine but i don’t think it’s right to call the PlayState so many times in the Update. I want that if it’s RUN,WALK or IDLE state to call it only once in the Update function.
Once walkbetweenwaypoints is true or false it will call the state nonstop in the Update function.