- Home /
Some help for "animation event triggers multiple times"
I use this script to trigger an animation. The event (OnSimpleTypeFinished) at the final clip would be called when the animation completes. I try to make a loop by applying this script whenever the previous finished, but it doesn't work. It since like the moment I add a new script, the OnSimpleTypeFinished would be triggered.
public class ActionSimpleType : Action
{
public GameObject obj;
private Animator animator;
private IActionCompleted monitor;
public void Setting (GameObject obj, IActionCompleted monitor)
{
this.ID = ActionID.SIMPLETYPE;
this.obj = obj;
this.monitor = monitor;
this.animator = obj.GetComponent<Animator> ();
animator.SetTrigger ("SimpleType");
}
public void OnSimpleTypeFinished ()
{
if (monitor != null) {
monitor.OnActionCompleted (this); // add the same script through this callback
}
Free ();
}
}
So I suppose that animation events would actually be called multiple times in a frame. A better way to avoid multiple callings would be like this:
public void OnSimpleTypeFinished ()
{
StartCoroutine (wait ());
}
IEnumerator wait ()
{
yield return new WaitForEndOfFrame ();
if (monitor != null) {
monitor.OnActionCompleted (this);
}
Free ();
}
Comment