Use of Unity Animation System

Basic usage

The Unity animation system uses a state machine, treats each animation as a state, changes the conditions in the state machine according to the user's input, and uses the specific conditions to control the switching of states, thereby realizing the change of animation. The following four steps will show how to use the animation system.

  1. Create a new Animator Controller in the Project and assign it to the Animator component on the character.
    Insert picture description here
  2. Open the Window->Animator window, drag and drop the animation (Animation) into the window to generate the animation state.
    Insert picture description here
  3. Set the transition and transition parameters between animation states, and pay attention to uncheck Has Exit Time. By default, the transition between animations is to check Has Exit Time, which means that the animation after the transition is automatically played after an animation is finished. Since we set the transition parameters, the transition from one animation to another will be realized when the conditions meet the transition, so there is no need to check Has Exit Time here.
    Insert picture description hereInsert picture description here
  4. Use code to control the transition between animation states.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCtroller : MonoBehaviour
{
    
    
    private Animator anim;
    private void Start()
    {
    
    
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
    
    
        if(Input.GetButtonDown("Vertical"))
        {
    
    
            anim.SetBool("Run", true);
        }
        else
        {
    
    
            anim.SetBool("Run", false);
        }
    }
}

Intermediate usage

Turn run

In this stage, we implement animations of running in different directions, including turning left and right, and running forward.

  1. Set the animation state, animation transition and transition parameters.
    Insert picture description here
  2. Write code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCtroller : MonoBehaviour
{
    
    
    private Animator anim;

    private void Start()
    {
    
    
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
    
    
        anim.SetBool("Run", false);
        anim.SetBool("RunLeft", false);
        anim.SetBool("RunRight", false);
        if (Input.GetKey(KeyCode.W))
            anim.SetBool("Run", true);
        if (Input.GetKey(KeyCode.A))
            anim.SetBool("RunLeft", true);
        if (Input.GetKey(KeyCode.D))
            anim.SetBool("RunRight", true);
    }
}

This method can realize that by holding down different keys, the character runs in different directions, but a problem will be found during the running process. The character will go through a process of standing upright when switching between running in different directions, which makes the animation look. It's not coherent.
Why is the animation inconsistent? Because there is no direct transition between running to the left, running forward and running to the right, they all need to pass through the Idle state as a transition, causing the character to stand upright before achieving different directions Running, in order to solve this problem, you need to add a direct transition to the animation state of running in different directions.

Use of AnyState

Any State represents any state, represents any state in the current layer in the animation system, and can be used to complete the continuity of the animation switching of running in different directions.

  1. Set the animation state, transition and transition parameters.
    Insert picture description here
  2. Write the code. Since the transition parameters have not changed, the code does not need to be adjusted.
    There are still some problems found here. When the character runs, it will freeze. This needs to adjust the mixing time ratio in the animation transition. This article will not elaborate.
    Insert picture description here

Advanced usage

Blend Tree

This part uses Blend Tree to realize the animation of the character running in different directions.

  1. In the Animator window, right click ->Create State->From New Blend Tree to create a blend tree.
  2. Enter Base Layer->Blend Tree, press the + sign in the Inspector panel, add three Add Motion Fields, and assign Run, RunLeft and RunRight animations to the three Motion Fields respectively.
    Insert picture description here
  3. Use the mouse to adjust the value of Blend in the Animator window, and you can see the character animation changes in the preview window of the Inspector panel.
    Insert picture description here
  4. In the Base Layer, rename the animation state Blend Tree to RunTree, and rename the parameter Blend to RunTree. Increase the transition from Idle to RunTree state, controlled by the Run parameter.
  5. Modify the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCtroller : MonoBehaviour
{
    
    
    private Animator anim;

    private void Start()
    {
    
    
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
    
    
        anim.SetBool("Run", false);
        if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.D))
        {
    
    
            anim.SetBool("Run", true);
            float dir = (Input.GetAxis("Horizontal") + 1) / 2;
            anim.SetFloat("RunTree", dir);
        }
    }
}

More topics

Curve
animation system events

Guess you like

Origin blog.csdn.net/Abecedarian_CLF/article/details/90544293