How to play idle animations only when player is idle
I have a script that determines if the player is idle.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class coheedAnimations : MonoBehaviour { public Animator coheedAnimator; public float MoveX; public float MoveY; public bool coheedIsIdle = true;
// Start is called before the first frame update
void Start()
{
//Get the Animator
coheedAnimator = this.gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
MoveY = Input.GetAxis ("Vertical");
MoveX = Input.GetAxis ("Horizontal");
coheedAnimator.SetFloat("MoveY", MoveY);
coheedAnimator.SetFloat("MoveX", MoveX);
if (MoveX != 0 || MoveY != 0)
{
coheedIsIdle = false;
}
else
{
coheedIsIdle = true;
}
} // end update
} // end class
However, when running, he will stop and still play a random idle animation. How do I get the state machine to only play when the boolean variable for idle is true?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RandomAnimIdleBehavior : StateMachineBehaviour { //public bool coheedIsIdle; public coheedAnimations isCoheedIdle; public string m_parameterName = "IdleAnimID"; public int[] m_stateIDArray = {0, 1, 2, 3};
override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
{
if (m_stateIDArray.Length <= 0)
{
animator.SetInteger(m_parameterName, 0);
}
else
{
int index = Random.Range(0, m_stateIDArray.Length);
Debug.Log(m_parameterName + "->" + m_stateIDArray[index]);
animator.SetInteger(m_parameterName, m_stateIDArray[index]);
}
}
}
[1]: /storage/temp/185134-statemachine.png
Your answer
Follow this Question
Related Questions
Animations are becoming distorted after an animation transition. 0 Answers
Animator transitions not working at all 2 Answers
Is it possible to use an avatar mask with the new AnimationClipPlayable et al.? 1 Answer
Movetowards Jerky motion; plays animation, then moves 1 Answer
8 way directional 2D attack animation? 0 Answers