Unity --- the use of character animation and button control character movement

1. Supplement to the previous article: the animator controller will not automatically get all the animation clips, if we want to add animation clips to the animator controller as the animation state, we have to be dragged by the corresponding animation clip file in the animator controller

Let's implement a function --- the character is standing idle under normal circumstances. When we press a button, the character will execute the pick up animation, and then execute the standing animation again after the animation is played. How to realize this function?

In addition to direct control through code, we can also control through the animator controller

Methods as below:

1. Right click on idle and select make transition --- Create transition 

If you select this in an animation state, the animation state will be connected by a yellow line and become a default playback animation

(There can only be one yellow line in one animator controller, and only one default play animation)

(After pressing the middle button of the scroll wheel, we can drag the screen of the animator controller)

2. After pressing Create Transition, a line will appear, connect this line with the animation state we want to transition, so that when the idle animation is played, the next one to play is the pick up animation (the default animation is always first played)

3. When executing the animation, if the animation does not play in a loop, then the animation in the animator player will be stuck in this animation state after the animation is played.

But when we transition like this

 After the idle animation is executed, it transitions to the pick up animation, and then after the pick up animation is executed, it transitions to the idle animation, thus forming an animation cycle, but! What we want is to execute the pickup animation when we press the button, instead of letting it loop the animation, so how can we do it?

1. Select the Paramenters parameter option at the top of the box below, where we can create parameters

Then click the plus sign in the picture below

After clicking, there are four types of parameters for us to choose, here we choose the trigger parameter

The trigger parameter is a trigger parameter

(Each transition line can be clicked, and various parameters of this transition line will appear in the inspector panel after clicking)

 The most important one in this parameter version is the conditons panel. In this panel, we can click the plus sign to add conditions (the minus sign is to delete the conditions) --- the conditions added here are the parameters we created

If the added parameter is a trigger parameter, this transition line will only be opened when the trigger parameter is in the triggered state, otherwise it will not be opened (ps: the trigger will automatically become non-triggered after being triggered once)

So how do we control the trigger parameters?

Control via script code

1. Create a script, and then attach this script to a game object that has an animator controller and performs animation

1. Classical if judgment operation after obtaining keyboard and mouse input in the updata method

2. After directly obtaining the Animator component, call the setTrigger method to set the trigger to the trigger state. The parameter passed in the parentheses of this method is the name of the controlled trigger (in the form of a string)

But the problem comes again, this method can not interrupt the current animation to directly execute the animation we want to execute

If the idle animation is being executed at this time, we just turn on the switch of the trigger after clicking the mouse, and then the transition is allowed to proceed, but if we really want to perform the transition operation, we must wait until the idle animation is executed.

So is there any way to forcibly stop the execution of an animation and go directly to another animation?

The answer is yes:

The transition process includes the exit time of the previous animation state and the start of the next animation state (the exit time of the previous animation state refers to the time left before the animation state ends)

We can choose to cancel the exit time of the previous animation, so that once the transition is started, it will go directly to the beginning of the next animation state

There is such a line selection in any transition panel. After we cancel this option, the animation will no longer have an exit time. Once the transition starts, the next animation state will be executed directly.

 

Guess you like

Origin blog.csdn.net/qq_51947882/article/details/126533778