0

I'm trying to play a single animation once on a character in Unity using MecAnim. I'm really not familiar with any of this, so I might have got the approach completely wrong.

The base layer contains a lot of walking stuff, but the animation I'm trying to trigger is on a seperate Right Arm layer with an empty Default state. There is a Wave trigger applied to the Default -> Wave transition, and a timed exit on the Wave -> Default one.

MecAnim - Right Arm Graph

I then have a script which (while debugging) fires when you press 'O' and causes the character to wave, once.

if (Input.GetKey(KeyCode.O))
{
    AnimatorStateInfo currentState = this.animator.GetCurrentAnimatorStateInfo(1);
    if (currentState.IsName("Right Arm.Default"))
    {
        Debug.Log("Waving triggered");
        this.animator.SetTrigger("Wave");
    }
}

The animation plays as expected, but the console shows that the IsName check is always passing as true, which makes me think I'm doing something wrong.

The in transition takes a fraction of a second to complete, then after that if the SetTrigger method is invoked again, the animation will play twice.

I'm looking for a simple way of ensure the trigger is only ever set when the state of this layer has fully returned to default.

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
Octopoid
  • 3,507
  • 5
  • 22
  • 43
  • Is there a particular reason why you're not using Parameters to control the state? checkout [`Animator.IsInTransition`](http://docs.unity3d.com/ScriptReference/Animator.IsInTransition.html) – Rob Feb 22 '16 at 15:11
  • Nope, still trying to work out what's best! I had assumed anything I wanted to happen on a one off basis was probably better suited to Triggers. If I use a bool parameter, do I set it back to false next frame, or do I need to track the animation time and set it back once it reaches 1.0? – Octopoid Feb 22 '16 at 15:14
  • 2
    Personally I would set a `Waving` bool on the animator & then check if the animator is or isn't still transitioning using `IsInTransition` Then you can do something like `if(anim.GetBool("Waving") && !anim.IsInTransition()` (This is all hypothetical) – Rob Feb 22 '16 at 15:16
  • Ok, I'm with you - just two questions really - I do want the anim to play only once as it's a gesture - do I need to track play time and set the bool to false at 1.0? Also, this layer will likely contain other gestures, Salute etc. - I only want any given gesture to play if none of the others are, is this still the right way? – Octopoid Feb 22 '16 at 15:20
  • 1
    1. yes, have a look at [this post on reddit](https://www.reddit.com/r/Unity3D/comments/1d3r1p/my_event_system_for_mecanim/) someone created a nice little event system for animations, it may come in handy here. 2. Yes typically I tend of have a different layer for discrete body parts that need to animate separately or additively. – Rob Feb 22 '16 at 15:48
  • 1
    Actually you could use the [mecanim built in events](http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html), and trigger a function on the clips timeline... – Rob Feb 22 '16 at 15:51
  • That's a great help, thank you - I think I'll use the wrapper so I can still use the looping animation separately to the one off gesture, but must remember about the animation events.. – Octopoid Feb 22 '16 at 15:55

0 Answers0