- Home /
machanim animation triggered twice
I was wondering if anybody has experienced an issue with mechanim where it plays an animation twice using SetTrigger. I have looked and seen a few people with this mostly in 2D games but not yet found a solution.
The anim is basically being called in an update my understanding is it works like a bool where it switches true then sets itself to false. The anims transition from idle using a condition to the ready anim then back to idle. If I add the condition to the ready back to idle transition it will fix the issue but I would rather work out whats going on than hack it
could this be an issue with using it in an update function?
here is the bit of code I am using I am having the same issue with another animation as well.
if (countdownText.text == "5") {
Debug.Log ("GameBegin");
animator.SetTrigger ("Get ready");
}
I have checked the animation state machine and I can see it running through twice
any thoughts on this?
The
if (countdownText.text == "5")
is that only true for 1 frame ? else SetTrigger will keep firing
I usually use something like ..
private var getReadyState = Animator.StringToHash("Base Layer.Get Ready");
private var currentBaseState : AnimatorStateInfo;
private var _animator = GetComponent.<Animator>();
function Start ()
{
_animator = GetComponent.<Animator>();
}
function Update ()
{
currentBaseState = _animator.GetCurrentAnimatorStateInfo(0);
callAnimation();
}
function callAnimation ()
{
while(currentBaseState.nameHash != getReadyState)
{
_animator.SetTrigger ("Get ready");
yield;
}
}
Or another way is call a boolean of 1 frame ..
PlayOneShot("Get ready");
/* ----------------------------
triggers the bool of the provided name in the animator.
the bool is only active for a single frame to prevent looping.
*/
function PlayOneShot (name : String)
{
_animator.SetBool(name, true);
yield WaitForSeconds(0);
_animator.SetBool(name, false);
}
// ----------------------------
Your answer