Control animation playback in Unity3D and unity3d animation playback

Source: Internet
Author: User

(Conversion) control animation playback in Unity3D and unity3d animation playback

It has been a long time to use Unity3D, but you can still learn new things every time you create a project. How can I play different animations in the upper and lower body at the same time? The solution is actually very simple, but the problem persists due to lack of understanding of animation resources. Finally, we thoroughly studied the Unity3D animation System (Legacy ), although a new animation control system was developed in 4.0, it was indeed much more powerful than the original one. However, this learning Summary of the original animation system was recorded, after all, the concepts and strategies of these animations are universal, and because 4.0 does not seem to have been cracked on mac, the project still needs to be implemented on 3.5. I believe it will be helpful for you to deeply understand the new animation system.

1. Unity3D animation system related Class 1.1 AnimationClip

AnimationClip is the most basic object for playing an animation in Unity3D. Each animation object imported through FBX is actually an AnimationClip. This class has recorded the position and rotation information of the skeleton joint on each time node in the form of a key frame, based on the frame frequency frameRate and the playback mode wrapMode, you can use interpolation to play a continuous skeleton animation.

1.2 AnimationState

Each AnimationState contains an AnimationClip and records some playback control attributes of this animation clip. It is actually an AnimationClip package.

The most important parameters are layer, weight, enabled, speed, and blendMode. The specific meanings of these parameters will be explained in detail in the Custom hybrid animation control. In simple animation control, you do not need to directly operate the AnimationState.

1.3 Animation

Animation is an Animation playback control component of Unity3D. It contains a series of AnimationState objects and provides various Animation playback and control methods. Common methods include Play (), CrossFade (), Stop (). In the script, you can directly reference the animation control on the same object through Animation.

 

Ii. simple animation playback control

The so-called simple animation playback control is only one action at a time, such as the plumber in Standard Assets, static idle, walk, run, jump, attack, all these actions are full-body actions, only one action should be performed at the same time. Only two switching actions need to be mixed during the action switchover to achieve smooth transition. The preceding operations use the CrossFade () provided by Animation () method. In either CrossFade or Play theory, you only need to call the animation once when switching. Of course, repeated calls do not affect the playing of the animation, this call is ignored when Unity3D detects that the currently played animation is the same as the animation used in function call.

 

3. Custom hybrid animation to control simultaneous playback of more than 3.1 animations

In most cases, simple animation playback control is sufficient. However, when the game becomes complex, the problem with single-animation playback is the dramatic increase in the workload of Fine Arts.

The most typical is a third-person shooting (Action) game. Generally, the characters in the game have various upper body actions, such as aiming, shooting, and changing bullets, while the lower body is standing, walking (usually in four directions ). At this time, if you can only play one action, you need to create a large number of actions (Standing aiming, standing shooting, running aiming, running shooting, etc.), the workload can be imagined. Obviously, the most efficient way is for the artist to perform the upper and lower half movements respectively, and then the program will mix the two movements according to the operation of the game role to play simultaneously. This involves the need to play multiple animations simultaneously. At this time, the simple CrossFade () method is no longer enough. We need to use the AnimationState to customize the animation playback control.

3.2 use AnimationState to control animation playback

The Animation playback of Unity3D is controlled by the AnimationState. The CrossFade and Play methods provided by the Animation component encapsulate a series of operations for setting the AnimationState parameter.

The main parameters are as follows:

Layer: The playing level of the animation clip (AnimationClip.

Weight: weight of the animation fragment in the animation mix (0 ~ 1)

Enable: whether the animation clip is played.

BlendMode: mixed mode. There are two types of Blend and Additive.

By default, layer = 0, weight = 0, enable = false for all animationstates In the Animation component during initialization. The Animation component defaults to 0, weight = 1, and enable = true.

 

I. Unity3D animation playback Policy

Unity3D follows the following policy during animation playback:

1. Find all animationstates on the top layer

2. Add the clip in the AnimationState where enable is true and weight> 0 to the hybrid pool (a fictitious concept)

3. If the weights of all clip whose blendMode is Blend are less than 1 in the current hybrid pool, all the animationstates of the next layer are found. Repeat 2.

4. Perform a hybrid operation on all clip in the hybrid pool to generate the final action.

During the final mixing, the actual weights of all clip will be normalized, that is, the sum is 1, and the results will be different according to different blendMode results. The following example will further explain.

 

Ii. Use AnimationState to control the playback instance

Here are a few examples to illustrate the Unity3D animation hybrid policy. All settings for the AnimationState are placed in Start or Awake. In addition, set the default playback Animation in the Animation component to None and deselect Player Automatically. The reason is that the default playback Animation initial value is different from other States, which may affect the experiment.

1. Mixed playback of the same layer of Animation

AnimationState right = animation ["run_right"];

AnimationState idle = animation ["idle"];

Right. layer = 1; right. weight = 1; right. enable = true;

Idle = 1; idle. weight = 1; idle. enable = true;

The running result is that the role animation is between walking and static, reflecting the mixed effect of the two animations.

2. overwrite the lower layer Animation

Right. layer = 1; right. weight = 1; right. enable = true;

Idle = 0; idle. weight = 1; idle. enable = true;

The running result is role walking.

3. Mixing High-rise and lower-level animations

Right. layer = 1; right. weight = 0.5f; right. enable = true;

Idle = 0; idle. weight = 1; idle. enable = true;

The running result is the same as 1. The actual weight of idle is 1 * (1-f)

4. Use Additive to mix different layers of Animation

Right. layer = 1; right. weight = 1; right. enable = true;

Right. blendMode = BlendMode. Addictive;

Idle = 0; idle. weight = 1; idle. enable = true;

The running result is the same as 1. The actual weight of idle is 1/(1 + 1)

 

Iii. Other animation playback control parameters

Speed: controls the animation playback speed. For example, you can increase the shooting speed.

Time: time of the current Animation

WarpMode: the animation loop mode. The animation loops PingPong.

3.3 Custom Animation transition

The animation transition is actually the transition of the two types of animation mixed weights. The weight of the previous animation is changed from 1 to 0, and the latter from 0 to 1, thus achieving smooth transition of the animation. In actual operation, you do not need to adjust the weight of the two animation animationstates in each frame of Update () during the transition. The Animation control provides the Blend () method to automatically calculate this transition in the background. Its usage is similar to that of CrossFade. For details, see the Unity3D script manual.

3.4 operations behind Play () and CrossFade ()

Understanding the operations behind these basic methods is useful when using the AnimationState to control the action at the same time.

The first is Play (stringanimationName) (Play () = Play (default Animation name). This method actually sets weight of all other animationstates in the layer where animationName is located to 0, set enable to false, set weight of animationName to 1, and enable to true.

CrossFade () is roughly the same as Play (), except that it does not directly set weight of other animationstates to 0, but calls the Blend () method to gradient it to 0, once the weight of the AnimationState changes to 0, set its enable to false.

It can be seen that when you set a multi-layer AnimationState, simply calling these two methods may not achieve the animation control you want. Of course, the two methods end with a default parameter PlayMode to adjust the execution policy (the difference is that the operation object is only at this layer or all the AnimationState objects ), in summary, this section describes the operations behind Play and CrossFade, hoping that we can correctly use these two methods to achieve the expected results when using AnimationState for mixed animation control programming.

3.5 partial Animation

The animation state operation described above only involves manual playback and mixed control of multiple animations, and does not involve how to play different animations to the upper and lower half of the body at the beginning. To play two different actions on the same role at the same time, use a local animation.

1. Generate a local Animation

There are two ways to implement local animation in Unity3D. One is to use the AddMixingTransform method of the AnimationState. This method is used to pass in the Transform of a skeleton node, after the call, the animation fragment AnimationClip will only affect the node and its Child bones, but will not affect other bone joints.

Another method is to make an animation for the local skeleton only when the animation model is created. Other bones remain unchanged throughout the animation segment, in this way, the animation fragment AnimationClip does not include key frame information for those unchanged bones, and does not affect other bone joints. (This Is My speculation based on the actual control results of animation playback. In the official Unity3D documents, it seems that it has not been directly stated and still needs to be verified)

Understanding the second method is very important, although it is just speculation. Before communicating with art, you may not know that a local animation is imported, but it is regarded as a full-body animation, which leads to unexpected results during mixed animation control. I found in a project that the role should enter the idle status, but the legs are still running. It took two days to verify the role, which directly contributed to the birth of this article, in the end, I found that the idle provided by the art only has an animation on the upper half and no animation on the lower half. Therefore, although the idle is higher than the running one, the lower half cannot be covered.

 

2. Mixing of local animations

The mixed policy of a local animation is the same as that of the Unity3D animation, but only affects the local skeleton. That is to say, the animation of the upper half and lower half will not affect the playing of the animation of the other half regardless of who is located on a higher layer and the weight. However, the upper-layer half-body animation will overwrite or mix the lower-layer full-body animation, while the bones not included in the lower-body animation are completely unaffected.

 

Iv. Shooting Game applications

Refer to the official Unity3.5 example Angry Robot. In this example, the upper and lower half bodies are rotated independently, and the shooting and running movements are separated from the lower half bodies. This section serves as an example for analysis.

Action Resource:

Run_forward, fun_backward, run_left, run_right. Four-way running, full body action, all placed on layer 1st.

Idle. Static, full body action, located at Layer 2nd.

Attack. Attack, half-body action, located at Layer 4th. Hybrid Additive

Operation Control:

Initialize. Set weight to 1 for all actions, enable to false for Attack, and true for others. In this way, Attack is not played in the initial state. The Idle overwrites the running action, and the role is displayed as a static standing action.

When moving, the weight of the Idle changes to 0 based on the speed to open the running action. Use CrossFade () to select an appropriate action from the four running actions for playback. When the task is stopped, the weight of the Idle is gradually set to 1 and the running action is overwritten.

Set the enable of Attack to true and false when the Attack is stopped. Because the Attack action is at the highest level and the mixed mode is Additive, the Attack action and the upper half of the walking action are mixed during the standing Attack, and the Attack action and the upper half of the running action are mixed during the running Attack.

 

Reprinted from http://blog.csdn.net/sparrowfc/article/details/8240165

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.