Mecanim Animation From Transform

In this post I will try to explain how I implemented simple auto animation for my characters based on their changing position in the scene (using mecanim and a simple script).



Position and Rotation only

In the above example the only thing being manipulated is the objects position and rotation
(The animations are happening automagically)

How to

First we need to load a Mecanim Animator->Controller and a model to attach an Animator component onto.

In my example I have used the classic unity model of Kyle the Robot and the standard ThirdPersonAnimatorController

(Any model and controller will do).

Standard Third Party Animation Controller on Kyle the Robot

Animation States and Variables

We can see that there are a few states and variables included in this controller.

The variable we are interested in is Forward.

ThirdPersonAnimatorController States

The value of Forward will determine the walk/run animation that mecanim wil animate for us.

The Code

Explanation

Each Update() the Velocity (Vector3) of the object is taken .

This value is then passed to the animator (if one exists) which sets the specified movement float (“Forward“ in my example)

Velocity

This is the difference between this frames transform and the previous frames transform.

NOTE -

MinMovementAmount - This variable can be changed in order to get the desired effect. I found that a value of 1 produced good movements for the prototype I was implementing.

Usage

Simply attach this script to your object that contains your character. Good luck, let me know in the comments if anything needs explaining or if this does not work for you :)


Comments: