"Unity3d Game Development Learning notes" (iv) Everything's moving. Application of-animator components

Source: Internet
Author: User
Introduction of Animation

Animation originally refers to by many consecutive pictures in front of the human eye fast playback, the naked eye because of visual remnants of the illusion, and mistakenly thought that the screen activities of the works. However, the "Animation" "animation" system in Unity3d should be interpreted in such a way that it is used to automatically play character movements or automatically demonstrate motion paths, color, size changes, and event-triggered integration components for players.
We can look at an example first to feel:

In the previous section we know how to build a terrain, add the Playercontroller resources of the system, now we simply build a terrain out, import Standardasset's third player controller, as shown below:

Just imported the third person controller, some students may encounter the problem of animation loss, such as figure to find

Drag the corresponding animation file in the resource folder into the script, and then run the game to see the villain move along with it. Second, Mecanim animation system

Mecanim animation System is a new animation system introduced by Unity3d, which has many new features, such as redirect, Fusion and so on, adding the concept of state machine. First of all, we have to understand the Mecanim system, we must first understand the following concepts: 1, animation file

The Animation file in Unity3d is a file that records the motion information of the object, and you can right-click the project file bar--> "create"--> "Animation" to create a Animation file, created after the file as a small icon of a clock. In addition, if you import the model files with animated information, Unity3d will automatically separate the animation information into Animation files.
The easiest way to use animation is to drag the animation into the gameobject you want to animate, and then select the Gameobject, press CTRL + 6, pop the animation window, right-click the timeline, and insert the keyframe. Then in the key frame to gameobject some of the properties of the change to edit it. 2, Avatar

Avatar, which is Avatar, is an interface for animation binding in the Mecanim animation system that enables the matching of skeletal and muscular systems to enable characters to move in accordance with pre-set animations when performing animations. First we import a model resources, the author here is the asset store inside the robot Kyle's resource package, specific import way: Landing Asset Store Download, the choice of unity in the open can automatically import, download the package generally stored in the c:/ Users/xxx/appdata/roaming/unity/asset store/unity technologies/directory.
Well, not so much, let's take a look at the model we're importing.

Arrow 1 points to our model resources in the Project panel, we select it, and we see three tabs in the Inspector panel: model, Rig, animations (arrow 2), click on the Rig tab to animation Type is set to humanoid, which is a bipedal type of animation. This type can support most humanoid-like models. In addition, the lagacy type is used to accommodate model animations in Unity3d versions, generic types are common types between humanoid and non humanoid models. After we select the humanoid type, select the type of build avater below to create Form this model, which is to generate a avater through the current model (Arrow 3 points)

After generating the avatar, we can edit and see if the skeleton bindings are correct by clicking Configure, or we can switch to the Muscles tab, which we can do by dragging the slider to check if the model match is correct. In this case, the model skeleton we imported does not have any problems, so just press done to complete the confirmation.
3, Animatorcontroller

After understanding and binding the avatar, we entered the learning of Mecanim's core components: Animatorcontroller.
If Avater is the interface that matches the body and skeleton of the model, then Animatorcontroller is the interface that binds animation and model implementations. We create a robotanimatorcontroller here. Double-click the file to open the Animator window, as shown below

Here will be a concept: States (animation state), in fact, this is a finite state machine, before entering each of the states, there will be a judge whether the condition entered, when the judgment condition reached, then meet the state, the state of animation playback. On the contents of the finite state machine, I do not intend to elaborate on this, we simply understand the following situation is good:
Entry (Animator Portal)->defaultstate (default)->next state (the next state that satisfies the condition)->exit (leave state)

Now let's learn how to use this animator.

As we see above, the above logo has three states (in fact there are four, and one exit is not truncated), in which Idle is a new state of our own, set to Defaultstate, which represents the animation that is executing it in normal condition. Right-click on the entry state, select Make Transition, and connect to the idle state. Then we click on the idle State, the Inspector panel in the motion animation of the binding (the following will explain the simple animation how to make, now we use the asset store inside the resources, the author is Mixamo, a free action resource package, It's pretty good. Select one of the idle animations to bind, after binding the animation, we go back to the scene panel, select the object that needs to add action, in the Inspector panel, click Addcomponent->animator, Then assign our robotanimatorcontroller to the corresponding attributes, as shown in the following figure:

Now, let's run the game to see. We can see that robot is already in the leisurely wandering up.

Finally put this lazy robot to move up, then we can't let him so relaxed, we have to find a way to let this lazy ghost run up, throw up. (Robot June: I ... ^%&...&*, can you let me be quiet as a handsome →_→)
We next, like the previous steps, in the animator interface, create a new two state and bind them to the animation, and then connect them to the idle as shown here:

Next, we add a status word to the animator, as shown in the arrow above, adding a status word of type int, named ActionId, with a default value of 0. Then, we select one of the transition, take idle->walk_front this transition as an example, we select it, and then see in inspector, in the condition, we add a judgment condition to the above, The argument is ActionId, the behavior equals, and the value is 1, meaning that when the current animator ActionId This value is 1, animator executes the Idle->walk_front state and stops the idle playback. Play Walk_front instead. (Here's an idea where, if you tick the has Exit time, you'll have a certain delay playing when you convert the animation, which depends on what everyone needs)
We've set the animator, so how do we change it in the game? Please look at the following code (the specific Code section will not say more, and then refer to the relevant API programming time to mention)

We create a new c#script, and then copy the following code, assigned to robot object, press the corresponding button to see the effect, the code is relatively simple, is to get the key pressed, and then change the animator inside ActionId value, I believe that we should have no big understanding of the problem.

Using Unityengine;
Using System.Collections;

public class Animation:monobehaviour {

    private animator M_animator = null;

    Use this for initialization
    void Start () 
    {
        m_animator = getcomponent<animator> ();// Gets the current object's animator
    }

    //update is called once per frame
    void Update () 
    {
        if Input.getkeydown ( KEYCODE.W))
            M_animator.setinteger ("ActionId", 1);
        if (Input.getkeydown (KEYCODE.K))
            M_animator.setinteger ("ActionId", 2);

        if (Input.getkeyup (KEYCODE.W) | | Input.getkeyup (KEYCODE.K))
            M_animator.setinteger ("ActionId", 0);
    }
}

Now let's look at the effect:

Well, not bad, although looking a bit ugly (robot June: →_→ blame me slightly. )
For more animator interface, please refer to: http://wiki.ceeger.com/script:unityengine:classes:animator:animator

Alas, the unity of the animation system is still very strong, I mentioned here are very basic, more things still need you to explore and use, which small partner with good to teach me _ (: З"∠)

Resource bundle download channel for landlord:
Action Resource Bundle: http://download.csdn.net/detail/nizihabi/9169503
Robot Model: http://download.csdn.net/ detail/nizihabi/9169557

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.