• How to control animation for 2D Object
How to control animation for 2D Object

Welcome to our Unity tutorial series.

In the previous tutorial, we created a simple animation for 2D object. Now I’m going to show you how to control it means we will make the animation activate whenever we want and switch between two or more animations of the same object.

 We’re keeping our project from our last tutorial. You can check that tutorial out or download only the project from this link.

After you’ve opened the project, if the current layout does not contain Animator window, you have to open it.

Click on Window menu -> Select Animator

 Drag to dock it somewhere like next toAnimation window

 

It opens the only animator you have automatically.

This animator is created by Unity when we create the animation for pacman object in previous tutorial. Unity also attaches an Animator component to that object.

You can see it in Inspector when click on its name in Hierarchy

 

 Get back to the Animator window. In our idea, we need the pacman object has two different states: Opens and closes its mouth when it’s moving and does nothing when it’s not moving. So we have to create one more state to show when it’s not moving. Just create another empty state like this:

Right click inside Animator window ->Create State ->Empty

A new empty state with its name is New State will appear. If you want to change the name of it, click on this new state then look at Inspector window:

 

Change the name New State to something like Idle then press Enter.

Also change the name of PacmanAnim state to something like Moving.

Come back to Animator window, when the game starts, the pacman object does not move. So we have to make the Idle state to become the default state.

Right click on Idle state ->Set as Layer Default State.

  

Now create two transitions between Idle state and Moving state.

Right click on Idle state ->Make Transition

 

A line from Idle state to mouse point will appear, click on Moving state to set destination for this transition.

Do it again for the second transition but from Moving to Idle.

You will see two lines connect between two states like this.

 

 Now we will need to know when this object will move.

Let’s create a parameter type bool to get its current state.

 

Click on Parameters on the top-left of Animator window to see the list of parameters.

Then click on the small plus sign to add new parameter with its type is Bool.

Set name for it is isMoving then press Enter.

 When the object is moving then the isMoving parameter’s value will be set to true, this animation state will change from Idle to Moving. To do this, we need to add a condition to the transition between two states.

Click on a small triangle in the center of the line has direction from Idle to Moving.

 

When its color change to blue, look at Inspector window and change some properties inside it like below:

 

Uncheck Has Exit Time

Click on small plus on the bottom-right -> Select the isMoving parameter -> Set value to True

 

Do it again for another line from Moving to Idle but remember to set the value of parameter to FALSE instead of true.

 

That’s all we need to do in Unity but we still have one more thing to do in script.

Open again the MovingObjectscript that we created and attached to pacman object from previous tutorial.

Add some lines of code like this:

//Get horizontal direction

float x = Input.GetAxis("Horizontal")* movingSpeed * Time.deltaTime;

//Get vertical direction

float y = Input.GetAxis("Vertical")* movingSpeed * Time.deltaTime;

 

if(x !=0|| y !=0)

{

    GetComponent<Animator>().SetBool("isMoving",true);

}

else//The same as if (x == 0 && y == 0)

{

    GetComponent<Animator>().SetBool("isMoving",false);

}

 

//Set new position for object

transform.position +=new Vector3(x, y,0);

 Let me explain about that if else statement:

if(x !=0|| y !=0): After we got x and y from above, if the object is moving then at least one of these variables must be different to 0. Then we will check if x or y is different to 0 or both of them are different to 0 (moving between two directions).

GetComponent<Animator>().SetBool("isMoving",true);

GetComponent<TypeOfComponent>() method is used to get the component which was attached to the same object as this script.

You can see both of them was attached to pacman object from Inspector window of Unity

 

 SetBool(parameterName, value) method is used to set value for bool parameter which was created from Unity.

You can see the line after if(x !=0|| y !=0) has the value is true means that when object is moving, we will set the value of isMoving parameter to true. And if the object is not moving (both x and y equal to 0), we will set it to false.

The last thing we need to do is press Ctrl + S (on Windows) to save the script (or build it)

Come back to Unity, press play button, using arrow keys to move object and see the result.

That’s the end of this tutorial, I hope you can make it without any problem. And it you have, don’t hesitate to ask me by leaving a comment.

Thanks for reading!

 

In the next tutorial, I will show you how to handle a collision in 2D game. See you!