- Home /
Model has Animator, how do I force start an animation, and tell where it is? (C#)
I've tried following a dozen different things online for this, and haven't gotten any of them to work (perhaps all out dated?)
Anyway, I have an FBX Model with assorted built in animations. I can get them to work by just having an Animation Controller loop back and forth between a few. But, I need an animation to be triggered in C#. I don't care about transitioning smoothly. I just want to instantly snap to the starting frame of an animation clip, then I need to be able to tell where it is in that animation. I have a method that needs to execute half way through the clip, and then another when the clip ends.
So far, I have not successfully gotten this to work, and it is proving to be a pain. Any help would be highly appreciated.
The easiest way to get feedback from the animation is to add an Animation Event, then listen for that Event in a Behaviour. Animation Events are added in Unity in the Animation timeline tab, and Behaviors are added in the Animator (state) tab. Your behavior will call a method with the same name as the timeline event.
Answer by DanVioletSagmiller · Jun 04, 2018 at 04:44 PM
Solution for 2018 was pretty straight forward for a code approach, once I figured it out.
// pseudo code
public Animator anim;
public AnimationClip clip;
[In Update]
if (press T)
- anim.Play(clip)
var state = anim.GetCurrentStateInfo(0);
if (state.normalizedTime > 1) - clip finished, call action
if (state.normalizedTime > .5f) - clip 50% done, call action
What I'm actually doing is not so basic, but it gave me what I needed to run this from code.