Unity Development Diary [Day 3] - Realization of Character Animation Effects

Table of contents

1. Standing and running animation effects

2. Jump animation

3. Some optimizations and corrections in the process of moving


1. Standing and running animation effects

In this section we try to add various animation effects to our characters

First add the component Animator to the character, we can see that this component requires a controller.

Next, create a new folder Animation under the root directory to store all animation effects, continue to create animation folders for the player and enemies in the folder, and create an animation controller named Player in the player folder.

Drag the controller to the corresponding position so that I can control the animation 

 

 In order to edit the animation we open the animation window from the upper window > animation > animation

After clicking Create, create an animation called Idle (standing), then find the standing animation in the material package (remember to adjust the number of pixels per unit before using it) and then drag the standing picture into it (you can drag the timeline method) Adjust animation speed)

You can also change the speed by modifying the sample number (we can view the current animation from Window > Animation > Animator)

In this way, we have completed the standing animation. Next, we will do the running animation. After the production is completed, we choose to create a transition to connect the two animations of Idel and run in the animator interface.

Click on the parameters of the animation, and add the parameter running (float type) to switch between the two animations.

Click the arrow from standing to running to cancel the exit time, set the transition time to 0, and set the condition to running greater than 0.1

Arrows on the other side are set accordingly.

Then open the player's script, just like the rigid body, we want to use the animation controller to get it first

public Animator anmi;

Then we drag the animation component to the corresponding position to use it

Put the following code into the Move function, where the function of SetBool is to set the value of running, here the judgment is based on whether the speed of the character is 0

anim.SetFloat("running", Mathf.Abs(rb.velocity.x));

2. Jump animation

Open the material package and you can see that the jump is two animations: jumping up and falling

Also add animation according to the content of the previous section

Then draw the logic of animation switching 

In order to control the switching process we set two variables (bool type)

 Also according to the previous process, there is no exit time and switching time, and then write the conditions to each of them, and then we start to edit the code, and put the following code after the judgment jump.

anim.SetBool("jumping", true);

So we add the jumping animation,

Next change the jump to fall condition 

There is also the state of returning from falling to standing. Here I have created a bool variable named ideling 

In order to realize that people can judge whether they fall on the ground, first create a layer named Ground [Note: It is not a sorting layer]

Then set the layer of the map to Ground

r then defines the following variables

public LayerMask ground;

Then set this as the layer Ground in the player 

 

Here we wrote a new function to realize this process, and optimized the original code at the same time. The complete code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public Rigidbody2D rb; //刚体
    public Collider2D coll; //碰撞体
    public Animator anim; //动画控制器
    public float speed; //速度
    public float jumpforce; //跳跃力
    public LayerMask ground; //碰撞体过滤

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Jump") )
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpforce);
            anim.SetBool("jumping", true);
        }
    }

    private void FixedUpdate()
    {
        SwitchAnim();
        Move();
    }

    void Move() //控制玩家移动函数
    {
        float Horizontaldirection = Input.GetAxisRaw("Horizontal"); //玩家移动方向
        rb.velocity = new Vector2(Horizontaldirection * speed, rb.velocity.y);
        anim.SetFloat("running", Mathf.Abs(rb.velocity.x));
        if (Horizontaldirection != 0)
        {
            transform.localScale = new Vector3(Horizontaldirection, 1, 1); //控制角色翻转
        }
    }

    void SwitchAnim()
    {
        anim.SetBool("idleing", false);
        if (anim.GetBool("jumping"))
        {
            if(rb.velocity.y < 0)
            {
                anim.SetBool("jumping", false);
                anim.SetBool("falling", true);
            }
        }
        else if(coll.IsTouchingLayers(ground))
        {
            anim.SetBool("falling", false);
            anim.SetBool("idleing", true);
        }
    }

}

Among them, IsTouchingLayers(ground) is used to judge whether the player's collision body collides with the ground, and then the logic is used to change various set parameters to realize the state change.

Then save and run the code and we will realize various action animations

3. Some optimizations and corrections in the process of moving

1. We also mentioned before that the problem that the character will get stuck or unable to climb the slope is because we use a square collision body. Here we change our thinking and use two collision bodies. The lower half uses a circle and the upper half uses a square , so that the problem can be solved and some other functions can be realized

2. All my projects write variables in public format and then drag them in. They can be changed to private mode and obtained through GetComponent in the Start function, which can reduce the number of parameters displayed on the Unity interface

Guess you like

Origin blog.csdn.net/qq_50688324/article/details/121127376