In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the usage of Animator components of Unity". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of Animator components of Unity".
Catalogue
I. Preface
II. Animator components
III. Animator Controller file
IV. Animation Clip file
State of the state machine (State)
1. Any State status
2. Entry status
3. Exit status
VI. Attributes of animation state
7. Transitional relationship between states (Transitions)
Add state control parameters
IX. Conditions for editing the switching state
Control status in the code
11. Check the animation status
Method 1. AnimatorStateInfo
Method 2. Inherit StateMachineBehaviour
Control the playback speed
XIII. Precautions
1 uncheck
(2) Action cycle.
3. Generally speaking, all we want is to switch immediately, so don't check it here.
XIV. Supplement
1 、 Mirror
2. Solo and Mute
I. Preface
Unity can control animation in two ways
1 Animation, this method is simple, directly Play ("Idle") or CorssFade ("Idle") can play animation
This method is recommended after 2 Animator,Unity5.x, because mixed animation can be added to make the animation switch smoother.
II. Animator components
When you create an Animation through the Create New Clip in the Animation window (the shortcut key is Ctrl+6), an Animator already appears quietly on the corresponding GameObject
III. Animator Controller file
On the Animator component generated in the first step, the first Controller parameter has already been assigned when the Animator is created. You can click on this value and switch to the Project window, and you will find that the file corresponding to this Controller is a .controller file.
Animator Controller is the animation controller, which is responsible for switching between different animations. It is a necessary original for making animation effects.
Note that you can also add a brand new Animator component through Add Component on GameObject, but in this case the Controller parameter of Animator is empty by default, so we need to manually drag the prepared .controller file to this parameter location for the animation controller to work properly.
IV. Animation Clip file
Double-click the .controller file and an Animator window pops up showing everything in the animation controller file (you can also open the interface via Window-Animator on the top toolbar)
Right-click in the Project window and select Create- > Animation to create an Animation Clip (.anim file)
Then drag the .anim file into the Animator window as a state of Animator Controller (State)
The Animation Clip created through Animator cannot be played directly through the hanging Animation component. If forced playback, Console will report a warning message:
The AnimationClip 'XXX' used by the Animation component' XXX' must be marked as Legacy.
And a prompt message.
Default clip could not be found in attached animations list
As follows
Why?
If we switch Inspector to Debug mode,
You can see that Animation Clip has a Legacy check box
Legacy means legacy, that is, the traditional practice of playing Animation Clip through Animation components. If you use Animation components to play Animation Clip, you must check Legacy. However, this method is outdated, and it is recommended to use Animator to play Animation Clip.
State of the state machine (State)
Each Animator Controller comes with three states: Any State, Entry, and Exit.
1. Any State status
Represents a special state of any state. For example, if we want the character to switch to a dead state in any state, then Any State can do it for us. When you find that a state can jump from any state to the same condition, then you can use Any State to simplify the transition relationship.
2. Entry status
Represents the entry state of the state machine. When we add an Animator component to a GameObject, that component begins to play its role.
If Animator Controller controls the playback of multiple Animation, which animation will the Animator component play by default? It's up to Entry to decide.
But Entry itself does not contain animation, but points to a state with animation and sets it as the default state. The state that is set to the default state is displayed as orange.
Of course, you can change the default state at any time with the right mouse button-> Set as Layer Default State on any state.
Remember that Entry jumps unconditionally to the default state after the Animator component is activated, and each Layer has one and only one default state.
3. Exit status
Indicates the exit status of the state machine, marked in red. If your animation controller has only one layer, then this state may not be useful. But when you need to return to the upper level (Layer) from the substate machine, just point the state to Exit.
VI. Attributes of animation state
We can select a custom state and observe its properties under the Inspector window
The property name describes the animation corresponding to the Motion state. The basic properties of each state, directly select the defined animation (Animation Clip) to Speed the speed of animation playback. The default value is 1, which means that the speed is 1.0 times the speed of the original animation. Mutiplier is available when you check Parameter on the right, that is, one of the parameters defined in area 1 is taken into account when calculating the Speed. If the selected parameter is smooth, the formula for calculating the animation playback speed is smooth * speed * fps (specified in animation clip) Mirror is only applicable to the periodic offset of humanoid animation (humanoid Mobile painting) Cycle Offset. The value range is 0-1.0, which is used to control the offset at the beginning of the animation. It can be understood by comparing it with the offset of the sine function, which will only affect the playback position of the initial animation. Foot IK is only applicable to humanoid animation (humanoid Mobile painting) Write Default. If you are interested, you can refer to the official manual Transitions for the list of transitions initiated from this state to other states, including two parameters, Solo and Mute, which play a role when previewing the effect of the state machine. Add Behaviour is used to add "behaviors" to states. 7. Transition relationship between states (Transitions)
Intuitively, they are directed arrows that connect different states.
To create a transition from state A to state B, right-click state A-Make Transition and drag the arrow to state B and click on the left side of the mouse.
Add state control parameters
The parameter has Float,Int,Bool,Trigger.
Float and Int are used to control the parameters of an animation state, such as speed and direction, which can be quantified numerically.
Bool is used to control the transition of animation state, such as from walking to running.
Trigger is essentially of type bool, but it defaults to false, and when the program is set to true, it automatically changes back to false.
Create a parameter AnimState of type Int here
IX. Conditions for editing the switching state
Click the connection, which can be set in the Inspecter window, and conditions can be added under the Conditions bar, as shown in the following figure when the parameters
The transition from animated Any State to New Animation2 will be performed when AnimState is 0.
You must add a parameter to the Parameters panel to see it here, and the second condition is the & & "and" relationship, which must be satisfied at the same time.
Control status in the code
We can set the conditional state through the code to achieve the purpose of animation switching.
Animator ator = go1.GetComponent (); ator.SetInteger ("AnimState", 0)
The above code, let AnimState this parameter value is 0, meet the transition condition from Any State to New Animation2, so as to achieve the transition of New Animation2 animation.
11. Check animation status method 1. AnimatorStateInfo
Add code to the script
/ / check whether the jump animation is being played. AnimatorStateInfo stateinfo = anim.GetCurrentAnimatorStateInfo (0); bool playingJump = stateinfo.IsName ("jump"); if (playingJump) {if (stateinfo.normalizedTime < 1.0f) {/ / playing} else {/ / end}}
When jump is in state, stateinfo.IsName ("jump") returns true
Method 2. Inherit StateMachineBehaviour
Each state of Animator can be mounted and created, which inherits from the StateMachineBehaviour class and is used to detect the running state of animation slices (Anamation) in the state machine.
Official example: https://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html
Just mount the script in the corresponding state. The code is as follows
Using System.Collections;using System.Collections.Generic;using UnityEngine;public class JumpState: StateMachineBehaviour {private GameObject player; override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {/ / the first frame in the state of played is called Debug.Log ("- OnStateEnter-") } / / 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) {/ / the last frame of transition to another state is called Debug.Log ("- OnStateExit-") } / / OnStateMove is called right after Animator.OnAnimatorMove () override public void OnStateMove (Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {/ / is called before OnAnimatorMove} / / OnStateIK is called right after Animator.OnAnimatorIK () override public void OnStateIK (Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {/ / is called after OnAnimatorIK for monobehavior of each frame in the playback state. / / it is important to note that OnStateIK is called only if the state is on a layer with IK pass. / / by default, the layer does not have an IK channel, so this function will not be called / / about the use of IK. See this article "Animator uses IK to achieve head and body following" / / https://www.jianshu.com/p/ae6d65563efa}} 12. Control playback speed Animator ator = go1.GetComponent (); var stateinfo = ator.GetCurrentAnimatorStateInfo (0) If (stateinfo.IsName ("Jump")) {ator.speed = 2;} XIII. Note 1 uncheck
Can Transition To Self, otherwise the animation will wobble.
(2) Action cycle.
Otherwise, if there is no next state switch, stop the action directly.
3. Generally speaking, all we want is to switch immediately, so don't check it here.
Has Exit Time. If checked, the switch is not allowed until the action is completed.
14. Supplement 1. Mirror
Mirror image, which can reverse the current animation and reduce the workload of animators
2. Solo and Mute
Mute is the equivalent of disabling the target transition. Solo said that only this transition will take effect.
You can choose more than one, and there will be an arrow prompt when selected.
Condition satisfaction takes precedence over Solo/Mute, and there will be no transition when the condition is not met.
Thank you for reading, the above is the content of "the usage of Animator components of Unity". After the study of this article, I believe you have a deeper understanding of the usage of Animator components of Unity, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.