[Ruimo.com] Solution to trigger animation multiple times in animator in unity

Two days ago when I was developing the game, I encountered a very mentally explosive problem. When I was doing a character animation control, I wanted to use the trigger to control the attack of the character, but during the test, I found that the attack animation would trigger twice. After a long time, I finally saw the solution in the video of MrFu's black soul re-engraving at station b.

Method steps

1. Suppose we have the following state control machine

From the dle state to the Attack state, a Trigger named attack needs to be triggered. Returning from Attack to Idle does not require any conditions. If the trigger is controlled, the animation of Attack can be played twice in a row to continue using the next steps.

2. Select Attack

Mount a new code named FSMCleaSignals for it, and paste the following code in the code page.

public class FSMCleaSignals : StateMachineBehaviour
{
    public string[] ckearAtEnter;
    public string[] ckearAtExit;
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        foreach (var signal in ckearAtEnter)
        {
            animator.ResetTrigger(signal);
        }
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        foreach (var signal in ckearAtExit)
        {
            animator.ResetTrigger(signal);
        }
    }

    // OnStateMove is called right after Animator.OnAnimatorMove()
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK()
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}

3. Go back to unity to adjust the parameters (you can also assign values ​​dynamically in the code)

Element is filled with the name of the Trigger you want to clear.

4. You can also mount the code on Idle

If the code is mounted on Idle, fill in the Trigger that needs to be cleared in CkearAtEnter.

The idea of ​​the full text comes from: https://www.bilibili.com/video/av21513489/?p=14

Guess you like

Origin blog.csdn.net/rrmod/article/details/128975291