- Home /
Manipulating 'Animator' speed to control animation / events
Hi!
I'm using a single animated sprite as an icon in our game world. Initially the animation sits with
animator.speed = 0;
till the user hovers over it, at which point it sets
animator.speed = 1;
I then have an animation event on the last frame of the animation, which sets
ticked = true;
The first frame then checks if 'ticked' is true and if it is, pauses the animation with
animator.speed = 0;
What then happens however, is immediately hovering over the sprite makes it first the event on the last frame on the animation. From what I can tell, it runs the last frames event as soon as you set the speed to 0.
Am I going about this wrong? The functionality I want is for the animation to play through to the end while the using is hovering, reverse to the beginning if they move away and run a function if they make it to the end of the animation. I thought what I had would work, but the events just seem to fire willy nilly.
Any help appreciated! Script below for reference.
void Start() {
animator = gameObject.GetComponent<Animator>();
animator.speed = 0;
}
void Update() {
RaycastHit hit;
bool isLookedAt = GetComponent<Collider>().Raycast(head.Gaze, out hit, Mathf.Infinity);
if (isLookedAt && !looked) //if icon is hovered and not yet playing, play
{
looked = true;
animator.speed = 1;
}
else if (!isLookedAt && looked) //if icon is playing but no hovered, stop
{
looked = false;
animator.speed = -1;
}
}
void endTrigger() //event on the last frame of the animation
{
open.SetActive(true);
ticked = true;
}
void checkTrigger() //event on the first frame of the animation
{
if (ticked) {
animator.speed = 0;
ticked = false;
}
}
Answer by wuzibu · May 27, 2015 at 10:35 AM
Seems like we're getting help with the release of 5.1 http://forum.unity3d.com/threads/mecanim-events-firing-incorectly-when-clip-is-playing-in-reverse-bug.226992/