Using the Unity Animation System

The exercise here demonstrates switching between animations in conjunction with mouse input.

The previous article discussed about the point and click mechanism and this article builds on it. So, please check it out before going further.

The objective at hand is to switch the animation to walk upon reception of the mouse input. As soon as the player object reaches the clicked position, the animation should switch back to idle. Simple!

The project at my hand has two animations already setup, idle and walk.

  • Create an animator controller in the project and attach it to the player object.
Unity
  • Open the controller in the animator window. Drag both premade animations there and create two-way transition in between them.
Unity
  • The transition shall be triggered based on certain condition and hence, add a bool or trigger as per preference.
Unity
  • Disable the exit time from the transition configs so that idle to walk is immediate.
Unity
  • Apply the same settings for the second animation shift from walk to idle.

Now, write some code in the player script to make this work.

  • In the section of player script which checks for mouse input, get the hold of the 3d model’s animator component.
C#

The math here is that as soon as the object reaches the clicked point, the animation shall switch back to idle. To measure the distance, an inbuilt function can be used and later on compared with a predetermined number to make this switch.

Unity

That should be it :)

Here is the final result !

Thank you very much

Work in Progress.