Mecanim uses state machines and code to control state changes

1. Place the animated model in the scene.
2. Create an Animator Controller in the project panel

3. Add the created Animator Controller to the Animator->Controller of the corresponding default animation.
4. Double-click the created Animator Controller. Open the state machine (Animator).
5. Some state machine settings.
6. Drag and drop the corresponding state directly into the state machine to generate the corresponding state directly.
7. There are 4 parameters in Parameters. These 4 parameters can be used to set the corresponding state opening conditions.
8. Use Float as an example. We set two parameters, Speed ​​and Angular, which are used to control his forward and steering.
When entering the Run0 state from the Idle state, there are 3 conditions, namely Speed>0.1 && Angular<0.1 && Angular>-0.1
When these three conditions are met, the state machine will transition from the Idle state to the Run0 state.
Also, if you want to go from Run0 to Idle state, maybe you need to set a condition.

There is an option for Has Exit Time. If the line is checked, even if the condition is met, the next animation will be executed after the animation is completed. If it is not checked, the operation will be performed immediately.
9. Use the corresponding script to control Speed ​​or Angular to set their values.
private Animator ani;
    void awake ()
    {
        ani = GetComponent< Animator >();
    }
        void Update () {
           ani.SetFloat( "Speed" , Input .GetAxis( "Vertical" ));
        ani.SetFloat( "Angular" , Input .GetAxis( "Horizontal" ));
    }
Ps: Use the keyword Awake to get the Animator's components before the program wakes up.
Animator has several methods. You can set the corresponding Parameters above.
They are:
ani.SetFloat("A",B);
ani,SetBool("C",D);
A: Indicates the name in the Parameter
B:Float property value
C: Indicates the name in the Parameter
D: Bool property value
10. Regardless of the current state, as long as the conditions are met, Dying's animation will be triggered.
This is the usage of Any State . In fact, it is equivalent to that each state points to Dying, and the trigger conditions are the same.
      ani.SetBool( "IsDie" , Input .GetKeyDown( KeyCode .Space));

PS: Respect originality, please indicate the source for reprinting.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326840879&siteId=291194637