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.

Recommended from Medium

Composition over inheritance

Construct phase spectrum before IFFT

BDD is bigger than I thought

Strapi with Firebase Auth — Owner’s Policy

Why We Chose Drone to Support Our 600 Deployments to Production Each Month

Tableau Rest API and Java.

Do not Cling to the Decisions Once Were the Best

10 Best Software Testing Company in the USA

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Samarth Dhroov

Samarth Dhroov

Work in Progress.

More from Medium

Animating Sprites In Unity

New Enemy Movement Behavior

Adding a Main Menu to our UI

Galaxy Shooter — Prototype to Production