[Unity Animation] Add parameters to the state machine to control animation switching (Animator Controller)

Unity - Manual: Animation Parameters

  Byleoyang@Digital media technology, plagiarism must be investigated

In the Unity animation state machine, we implement the playback of animation clips. When we have multiple animation clips, the state machine is like a dispatch center that can control the playback of different animation clips (such as walking, running, shooting, squatting, defense, attack actions). But different animation clips need to be switched directly. Since switching requires transition, we use a line in the state machine to represent the transition between the two animations.

These transitions can make the switching between two animations more natural, just like adding technical transitions (cross-dissolve, etc.) to the two videos in PR editing.

And you can create some public parameters (Parameters), store the state machine corresponding to the game object through the Animator class, and then call setBool ("parameter", the parameter value you specify), setFloat ("parameter", the parameter value you specify), setIntger ("parameter", the parameter value you specify) to set.

Parameters can be added to each line as conditions to control the switching of animations!

In Unity, the switching of animation states is achieved through the transition in the Animator Controller. Transition is the connection between states. Controlling transition generally relies on calling code parameters

Let's implement a case:

1. Create an animation state machine (Animator Controller): In Unity, you can manage the animation state by creating an Animator Controller. In the Unity editor, right-click on the Assets window and select Create -> Animator Controller. Then, give it an appropriate name, such as "MyAnimatorController".

2. Create animation states: In the Animator Controller, you can add various animation states. Each state represents an animation segment, such as standing, walking, jumping, etc. You can create these states by dragging animation assets into the Animator Controller.

3. Set transition: Establish transition relationships between states. Select a state, then right-click, Make Transition, and a line will be created that points to the animation clip to be transitioned.

4. Set transition conditions: Select the animation transition line you want to control, and then the properties of the transition line pop up on the right. Click the plus sign in Conditions (trigger conditions) at the bottom to automatically The newly added public variables on the left pop up. You can choose whatever you want. It is best to match them.

 5. Use scripts to control parameters: In the game script, you can modify the values ​​of animation parameters by calling the methods of the Animator component. For example, use animator.SetFloat("Forward", v); to set the value of the floating point parameter named "Forward" to the variable v.

 6. Trigger state switch: When the value of the animation parameter meets the transition condition, the Animator Controller will automatically trigger the state switch. This may result in a transition from one state to another, with the corresponding animation playing.

passed in script

animator.SetFloat("Forward", v);

animator.SetFloat("Strafe", h);

animator.SetBool("Fire", fire);

The value of the animation parameter is set to the input variable, which may trigger a state switch in the Animator Controller.

  • Integer - Integer
  • Float - Number with decimal part
  • Bool - true or false value (represented by a checkbox)
  • Trigger - A boolean parameter that is reset by the controller when the transition is used (represented by the round button)

Can be used as an Animator function in the script. /span> 和 < /span> . ResetTriggerSetTrigger, SetBool,

Full code reference

using UnityEngine;
using System.Collections;

public class SimplePlayer : MonoBehaviour {
    
    Animator animator;
    
    // Use this for initialization
    void Start () {
        animator = GetComponent<Animator>();
    }
    
    // Update is called once per frame
    void Update () {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        bool fire = Input.GetButtonDown("Fire1");

        animator.SetFloat("Forward",v);
        animator.SetFloat("Strafe",h);
        animator.SetBool("Fire", fire);
    }

    void OnCollisionEnter(Collision col) {
        if (col.gameObject.CompareTag("Enemy"))
        {
            animator.SetTrigger("Die");
        }
    }
}

Note: In addition to controlling the transition of the animation, the parameters we set ourselves can also control the action switching within the animation mixing tree. These parameters are like the global variables you declare in the code. They do not belong to any one person, but are a public " switch"

Other purposes and uses of parameters:

  1. Response to user input: In the script you provide, by getting user input, such as horizontal and vertical input, and button presses, these input values ​​are used as animation parameters. In this way, the animation system can control the animation playback based on the user's actual operations, making the animation more vivid and interactive.

  2. Triggering by external events: Animations can be triggered not only by user input, but also by events in the game. For example, in the script, when the character collides with an enemy, the trigger parameter named "Die" is triggered, which causes the animation state machine to switch to the death state. This way, animations can be adjusted accordingly to specific events in the game.

  3. Conditions for animation state switching: In the animation state machine, switching between states usually needs to meet some conditions. These conditions can be that the value of an animation parameter reaches a certain threshold, or that a trigger parameter is activated. By setting parameters, you can flexibly control the switching logic between states.

  4. Implement animation blending: When using a blend tree (Blend Tree), parameter changes can be used to control the weights of different animation layers. This allows for smooth transitions and blends, making animation transitions more natural. (see subsequent tutorial)

In general, setting parameters allows the animation system to be interconnected with game logic and user input, so that the animation can be dynamically adjusted according to changes in external conditions during runtime, thereby enhancing the expressiveness and interactivity of the animation. This dynamic adjustment and response is key to vivid and smooth animation in games.

Guess you like

Origin blog.csdn.net/leoysq/article/details/134762203