Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ataxkt · Nov 18, 2013 at 07:33 PM · animationanimationevent

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);
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wiatrog · Dec 24, 2013 at 08:42 PM 0
Share

I'm having this same issue. Any solution?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
             }
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges