Mecanim Animation Bridge - 3D

The Mecanim 3D animation bridge sets parameters in a standard mecanim state machine, it does not play states directly. You can use these parameters to drive transitions in the 'standard' mecanim manner.

Tip: Although the bridge is called 3D it can be used to do complex 2D animation too. For example see the Pixtroid sample scene.

Tip: The Mecanim 3D animation bridge requires an understanding of mecanim. If you are starting out you may want to start with Unity's mecanim tutorials. 

 

Required Components

 In order for the Mecanim 3D bridge to animate it requires:

  • An Animator component.
  • A SpriteRenderer or MeshRenderer or SkinnedMeshRenderer (and associated components).

 

Quick Start

The easiest way to create your 3D animations is to create transition from the Any State state to each animation state. You set the condition for this animation to the state number that matches the AnimationState.

For example the WALK state from the AnimationState enum has an integer value of 100. So you would set up walk with the following transition:

Can Transition To Self must be false (else the start of the animation would be repeated). Usually you would also set Atomic to false so that the animation can be interrupted.

You can find the integer value of an AnimationState enum by opening the .cs file AnimationState:

 

Tip: It can help to have this open in a sticky window while setting up animation.

 

State to State Transitions

In order to customise the transition between two animations states (such as the transition time) you need to do two things:

Step one - Create a transition between the two states

This is identical in set up to the transition between the Any State transition with a condition set on the State variable matching the integer value from the AnimationState enum.

Step two - Prevent the Any State transition from triggering

Let imagine two transitions:

  Any State to JUMP (the standard transition)

and 

  WALK to JUMP (the new transition we just added)

We now have two ways to get between WALK (which matches both WALK and Any State) and JUMP. So we need to prevent the Any State to JUMP transition if the previous state was WALK.

We can do this using an extra condition on the Any State transition which requires the PreviousState to be something other than WALK:

So now our Any State to JUMP transition only occurs when the current state is JUMP (200) and the previous state is not WALK (100).

 

Transitional Animations

You can add transitional animation in between state animations using the same technique as above. For example to add an animation for landing we need an animation between the FALL state and the IDLE state. To do this we do the following:

1. Create a new state called LAND. 

2. Create a transition from FALL to LAND.

3. Set the condition on this transition to be: State Equals 0 (IDLE).

4. Create a transition from LAND to IDLE. 

5. Set the condition on this transition to be the exit time (we want the landing animation to finish then to walk as normal).

6. Finally guard the Any State to IDLE transition by adding a new condition (as per above) so that it doesn't play if the PreviousState was FALL. The condition would be: PreviousState NotEqual 220 (FALL)

Tip: You would probably want to add additional transitions from LAND to other states like WALK or RUN.

 

Blend Trees

Blend trees can be set-up as per standard mecanim animation practices. The Stealth Sample has a simple blend tree between WALK and RUN based on the setting of the VelocityX parameter.

 

Available Parameters

The following parameters are made available to the mecanim animator: 

State Animation state last sent by the character.
PreviousState Animation state that was in effect prior to the current state.
HasPlayed Tracks if the current animation has played for at least one frame (this is an advanced option, see details below).
GunPositionX X value of the aiming direction. Only set if a ProjectileAimer is attached to the character.
GunPositionY Y value of the aiming direction. Only set if a ProjectileAimer is attached to the character.
VelocityX Character velocity in X. As set by the movement, remember that movements can interpret velocity in different ways (relative, world, or something custom).
VelocityY Character velocity in Y. As set by the movement, remember that movements can interpret velocity in different ways (relative, world, or something custom).
FacingDirection Int value for facing direction 1 = right, 0 = none, -1 = left.

 

Exposing new Parameters

With a little code you can easily extend the Mecanim 3D bridge to expose new parameters to help improve your animations. This code sample below creates a new bridge which exposes character health and slope rotation as new parameters for your mecanim animator.

 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace PlatformerPro
{
    /// <summary>
    /// An animator that sends more parameters to the mecanim controller than the standard bridgeThis is primarily
    /// here as an example of how you can extend the Mecanim 3D bridge.
    /// </summary>
    public class MecanimAnimationBridge_3DExtraParams : MecanimAnimationBridge_3D
    {
        protected CharacterHealth characterHealth;

        /// <summary>
        /// Init this instance.
        /// </summary>
        override protected void Init()
        {
            base.Init ();
            if (myCharacter is Character)
            {
                characterHealth = ((Character)myCharacter).GetComponentInChildren<CharacterHealth>();
            }
        }

        /// <summary>
        /// Unity Update hook.
        /// </summary>
        override protected void ActualUpdate()
        {
            base.ActualUpdate ();
            myAnimator.SetFloat("SlopeRotation"myCharacter.SlopeActualRotation);
            if (characterHealth != null)
            {
                // Percentage health we could for example use this to blend in limping animations as the character takes damage.
                myAnimator.SetFloat("Health"characterHealth.CurrentHealthAsPercentage);
            }
        }
    
    }
}

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk