- Home /
Animation events won't trigger if animation time set manually
Hi everyone! I've created a little script that lets me scrub through animations. To do this I've set the animation speed to 0, and set it playing. Every frame I then set the animation's time to the value I get from my scrubber.
However, it seems that with animation speed at 0, my animation events won't trigger, even when the animation time is explicitly set to match the animation event time. When I set the animation speed to 0.000001 (or whatever value) it works, but obviously I don't really want to do this. I assumed Animation.Sample() would fix this, but it hasn't (it doesn't actually seem to do anything).
Has anyone hit this before? Otherwise I'll mark it as a bug.
The entire project's code is below for anyone who wishes to test.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animation))]
public class Example : MonoBehaviour {
public float animationTime;
public float scrubber;
public string animationClipName = "My Name";
public Vector3 pathPosition;
public new Animation animation;
public AnimationClip clip;
public AnimationCurve curve;
public AnimationState state;
public AnimationEvent animEvent;
void Start() {
animation.wrapMode = WrapMode.Loop;
clip = new AnimationClip();
clip.SetCurve("", typeof(Example), "pathPosition.x", curve);
clip.name = animationClipName;
animEvent = new AnimationEvent();
animEvent.time = 1;
animEvent.functionName = "AnimationEventReciever";
animEvent.stringParameter = "Woooo. Strings!";
animEvent.messageOptions = SendMessageOptions.RequireReceiver;
clip.AddEvent(animEvent);
animation.AddClip(clip, clip.name);
animation.Play(clip.name);
animation[animationClipName].speed = 0;
state = animation[animationClipName];
}
void Update(){
animationTime = animation[animationClipName].time;
animation[animationClipName].time = scrubber;
animation.Play(clip.name);
//animation[animationClipName].speed = 0.000001f;
animation.Sample();
}
public void AnimationEventReciever(string _stringParameter){
print(_stringParameter);
}
}
Answer by SAM-tak · Dec 13, 2016 at 11:45 AM
you will need to trigger animation event manually too, when you set a animation time manually.
foreach(var i in animation[animationClipName].clip.events) {
// fire animation event
if(animationTime< i.time && i.time <= scrubber) {
foreach(var j in gameObject.GetComponents(typeof(MonoBehaviour))) {
try {
var mi = j.GetType().GetMethod(i.functionName);
if(mi != null) {
var pers = mi.GetParameters();
if(pers != null && pers.Length > 0) {
if(pers.Length == 1) {
if(pers[0].ParameterType == typeof(string)) {
mi.Invoke(j, new object[] { i.stringParameter });
break;
}
else if(pers[0].ParameterType.IsByRef) {
mi.Invoke(j, new object[] { i.objectReferenceParameter });
break;
}
else if(pers[0].ParameterType == typeof(float)) {
mi.Invoke(j, new object[] { i.floatParameter });
break;
}
else if(pers[0].ParameterType == typeof(int)) {
mi.Invoke(j, new object[] { i.intParameter });
break;
}
}
}
else {
mi.Invoke(j, null);
break;
}
}
}
catch(System.Reflection.AmbiguousMatchException e) {
Debug.LogError("Cannot find animation event method : " + i.functionName);
}
}
}
}
Your answer
Follow this Question
Related Questions
How to add new curves or animation events to an imported animation? 6 Answers
Abrupt change of position between animation transition 2 Answers
AnimationEvent has no receiver, but there is no AnimationEvent 1 Answer
Rotating arm with Animation Event 1 Answer
Can the animation editor create local rotational data? 3 Answers