Project Training--Unity Multiplayer Game Development--Part Five

Animation system for character control

review

In the last two articles, we mainly talked about several ways of character control. This time, we will explain how to control the animation of the character's movement after the character is controlled and the required animation.

Project scenario:

提示:主要是针对第一个游戏的动画来探讨动画系统:

animation controller
Animation Control Parameters

The above two diagrams mainly involve the execution flow and control conditions of the animation. For the character who has just entered the static state of maintaining an idel, we need to make a judgment, and judge whether it is necessary to perform a moving animation by pressing the button. There are four variables on it, which control different transitions respectively. The first is speed, and the second is judgment. Whether it is on the ground, the third is whether to jump, and the fourth is whether to die.


describe

在该角色中,我们使用了移动加速和跳跃以及下坠的动画,这就需要保证他们的一致性,需要保证条件能够正常转换,以下是一些判断条件

Animator anim;  //动画
start中
rb = GetComponent<Rigidbody2D>();
update中
isOnGround = Physics2D.OverlapCircle(groundCheck.transform.position, checkRadius, platform);//判断是否在地上(在平台上)
 anim.SetBool("isOnGround", isOnGround);

Regarding the moving animation, use a float value to manage, control whether to switch the moving animation or running animation, or keep the static animation.

 rb.velocity = new Vector2(xVelocity * speed, rb.velocity.y);

        anim.SetFloat("speed", Mathf.Abs(rb.velocity.x));//跑动动画

Execute the jump animation. In the OnCollisionEnter2D method, we need to judge the conditions under which the player can perform the jump. The first is to be on the ground, and the second is to press the w key, or it can be executed automatically on the ground of a fan jump. Only in these two cases will it jump. Otherwise it won't jump. If it collides with something labeled "Fan", the player maintains the original horizontal speed while gaining an upward speed of 10float.

  if (other.gameObject.CompareTag("Fan"))
        {
            rb.velocity = new Vector2(rb.velocity.x, 10f);
        }
up = Input.GetAxis("Vertical");
        if (up > 0.1 && isOnGround && !other.gameObject.CompareTag("Fan")) //判断是否在地面上,否则不能跳跃
        {
            rb.velocity = new Vector2(xVelocity * speed, yVelocity * 6.0f);
            anim.SetFloat("up", up);//跳跃动画
            Debug.Log(up);
        }

To complete the injury operation, a trigger judgment is required, and istrigger is used to judge whether the death condition is met. The death condition is to touch a spiked object, and the character performs a death animation.

  if (other.gameObject.CompareTag("Spike")) //碰撞到一个标签为spike的物体、铁刺、在unity场景中可以看到TopSpikes内的idle,选中idle就看到inspector里显示着tag:spike
        {
            anim.SetTrigger("dead"); //触发dead的动画
            //PlayerDead();
            //playerDead = true;
        }

solution:

For the execution of the animation, there may be a phenomenon of delaying the animation. We use the method of closing the exit time to realize the switching of the animation:

insert image description here
As shown in the figure, uncheck hasExitTime. This makes animation switching faster. There will be no delay in animation.

Summarize

This time I mainly talked about some variables in character control, the logic of execution, and some problems encountered. Through this study, I mainly learned about a way (also the most common way) of unity to control animation. Simple and logical.

Guess you like

Origin blog.csdn.net/qq_53259920/article/details/125124398
Recommended