Skip to content

[Unity] Use Animator to Animate the Character to Move

Last Updated on 2021-11-01 by Clay

The article take 2D game as an example to implement the animation of character running in Unity.

If you want to implement animation effect in Unity, you need to know there are two types of animation components: Animator and Animation.

  • Animator: It is the animation controller, usually there is only one on a character
  • Animation: It is animation, Running is a group of animations, and jumping is also a group of animations. There may be multiple animations on a character

The judgment of switching between different Animations (switching between different animations) must be completed by using Animator.


Preparation

I have prepared four pictures as examples, which can be downloaded and used directly.


Then open the Unity project, create the ground and character.

Box Collider 2D need to add to floor, Box Collider 2D and Rigidbody 2D added to character.


Animation

Step 1: Add Animator to character


Step 2: Create an Animation

Window > Animation > Animation open Animation window.


Create "Idle Animation" and "Running Animation".

Click Create to start creating.


The Idle action is just a picture of standing idle, there is nothing to say; the running animation is four pictures: middle movement > stepping out of the right foot > middle movement > stepping out of the left foot.


Step 3: Adjust Animator

Then we adjust the Animator on the character object.

Set the PlayerIdle animation to Set as Layer Default State. This means that when the game scene is started, the character defaults to execute this animation repeatedly.

Since we only have one Idle picture, you will see the character just standing blankly.


Then, use Make Transition to make the transition of different animations. We need to set the switching flow direction back and forth, so as to ensure that the characters can be changed from Idle to running, and also from running to Idle.


Then add a condition: select the Parameters page and add an integer variable of Status.


Then select the Traditions line that switches from Idle to running. In the Inspector column on the side, cancel Has Exit Time. If we don't cancel it, out animation will not switch to the next animation until the end of the broadcast, which will make the characters actions look stuck.

Then add a condition below and select Status equal to 1 as the condition. After that, we only need to change the value of Status in the program to smoothly change the character animation.


The other line that switches from running to idle does the same thing, except that the condition of Status needs to be changed to 0.


Step 4: Write the character movement script

Attach PlayerMovement.cs to Player game object.

myAnimator is an Animator object used to control the Status variable of the animation; and mySpriteRenderer is picture object used to mirror the image to another direction.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // Init
    public float moveSpeed = 0.2f;

    // Properties
    private Animator myAnimator;
    private SpriteRenderer mySpriteRenderer;

    void Start()
    {
        // Animator
        myAnimator = GetComponent<Animator>();

        // Sprite
        mySpriteRenderer = GetComponent<SpriteRenderer>();
    }

    void FixedUpdate()
    {
        // Movement
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            mySpriteRenderer.flipX = true;
            myAnimator.SetInteger("Status", 1);
            transform.position -= new Vector3(moveSpeed, 0);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            mySpriteRenderer.flipX = false;
            myAnimator.SetInteger("Status", 1);
            transform.position += new Vector3(moveSpeed, 0);
        }
        else
        {
            myAnimator.SetInteger("Status", 0);
        }
    }
}


Output:

https://clay-atlas.com/wp-content/uploads/2021/07/Screen-Recording-2021-07-03-at-12.03.25-PM.mov

So far, the animation control to be recorded today is almost over. If the character has more actions that you want to make, of course, you can make different Animation animations, use Animator to switch between different animations, use conditions to determine which animation should be switched to now… etc. .


References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version