[Unity] U3D ARPG game production example (2) character basic action switching


Preparation

Open the prepared scene.
insert image description here

Add the EasyTouch joystick and buttons from the previous chapter to this scene.
insert image description here

Import a character model:
insert image description here

Put the model under the empty node:
insert image description here
hang the script of the previous chapter under the empty node player, and add the Character Controller component:
insert image description here
see the effect:
Please add a picture description

Unity operation

Create an AnimatorController folder under the character directory, and create an Animator Controller named PlayerAnimatorController under the folder:
insert image description here

Add the Animator component to the character model:
insert image description here

Drag the Avatar of the character model and the created PlayerAnimatorController into the Animator component:
insert image description here
If there is no Avatar, you can create one yourself: select the Rig tab in the model, specify Avatar Definition as Create From This Model, and click Apply. Of course, it is best to use an Avatar that matches the model, otherwise the action is likely to be deformed.
insert image description here

Open PlayerAnimatorController, right-click on the blank space to create a blank state named Idle (Create State --> Empty), and then set this state as the default state (Set as Layer Default State), usually the first state created will be Automatically designated as the default state, the yellow state is the default state.
insert image description here

Then create two states of Die and Run. According to the figure below, right-click each state to connect the moving line to the corresponding state (Make Transition).
insert image description here

Switch to the Parameters tab and add three bool variables (idle, run, die).
insert image description here

Left-click to select the Idle state, and drag the animation corresponding to the stopped state to the Motion field. In this way, animations are also assigned to Die and Run respectively.
insert image description here

Select a single animation file and click the Edit button (Edit).
insert image description here
Check Loop Time so that the animation can be played repeatedly, then click Apply to save the changes. Both Idle and Run need to do this, but Die doesn't, unless your character needs to die non-stop.
insert image description here

Left-click to select a moving line (Transition), uncheck Has Exit Time to ensure that the animation can be switched immediately. Then add a condition below Conditions, the condition from Idle to Run is that run is true; the condition from Run to Idle is run is false; the condition from Any State to Die is die is true.
insert image description here

Note that there is a transition time between animations, drag and drop here to adjust the transition time.
insert image description here

code adjustment

Change the code of CharacterMotor in the previous chapter to:

using UnityEngine;

namespace ARPGFinish.Character
{
    
    
    /// <summary>
    /// 角色马达
    /// 用于处理角色移动
    /// </summary>
    public class CharacterMotor : MonoBehaviour
    {
    
    

        /// <summary>
        /// 动画系统
        /// </summary>
        private CharacterAnimation chAnim;

        /// <summary>
        /// 角色控制器
        /// </summary>
        private CharacterController chController;

        /// <summary>
        /// 移动速度
        /// </summary>
        public float moveSpeed = 5;

        /// <summary>
        /// 转向速度
        /// </summary>
        public float rotationSpeed = 0.5f;

        /// <summary>
        /// 重力
        /// </summary>
        private float gravity = 2;

        /// <summary>
        /// 跳跃速度
        /// </summary>
        public float jumpSpeed = 3;

        /// <summary>
        /// 跳跃时间
        /// </summary>
        public float jumpTime = 0.5f;

        /// <summary>
        /// 累计跳跃时间用来判断是否结束跳跃
        /// </summary>
        public float jumpTimeFlag = 0;

        /// <summary>
        /// 是否移动
        /// </summary>
        private bool isMoving = false;

        /// <summary>
        /// 是否跳跃
        /// </summary>
        private bool isJump = false;

        private void Start()
        {
    
    
            chAnim = GetComponent<CharacterAnimation>();
            chController = GetComponent<CharacterController>();
        }

        /// <summary>
        /// 移动方法
        /// </summary>
        public void Move(float x, float z)
        {
    
    
            if (x != 0 || z != 0)
            {
    
    
                isMoving = true;
                // 1转向 前往的方向
                LookAtTarget(new Vector3(x, 0, z));
            }
        }

        /// <summary>
        /// 转向
        /// 通过Lerp实现转向
        /// </summary>
        private void LookAtTarget(Vector3 target)
        {
    
    
            if (target != Vector3.zero)
            {
    
    
                Quaternion dir = Quaternion.LookRotation(target);
                transform.rotation = Quaternion.Lerp(transform.rotation, dir, rotationSpeed);
            }
        }

        /// <summary>
        /// 停止移动方法
        /// </summary>
        public void MoveFinish()
        {
    
    
            isMoving = false;
        }

        /// <summary>
        /// 角色跳跃
        /// </summary>
        public void Jump()
        {
    
    
            isJump = true;
        }

        private void Update()
        {
    
    
            //float distance = Vector3.Distance(dir.GetTargetPosition(),this.transform.position);
            if (isMoving)
            {
    
    
                chController.Move(transform.forward * moveSpeed * Time.deltaTime);
                // 3播放运动动画  
                chAnim.PlayAnimation("run");
            }
            else
            {
    
    
                chAnim.PlayAnimation("idle");
            }

            // 如果在跳跃状态,并且当前时间没有到达跳跃时间,则持续向上移动如果到达跳跃时间,那么持续下降直到遇到地面
            if (isJump)
            {
    
    
                if (jumpTimeFlag < jumpTime)
                {
    
    
                    chController.Move(transform.up * jumpSpeed * Time.deltaTime);
                    jumpTimeFlag += Time.deltaTime;
                }
                else if (jumpTime < jumpTimeFlag)
                {
    
    
                    chController.Move(transform.up * -gravity * Time.deltaTime);
                }

                if (chController.collisionFlags == CollisionFlags.Below)
                {
    
    
                    jumpTimeFlag = 0;
                    isJump = false;
                }
            }
        }
    }
}

Add a new script CharacterAnimation in the Character folder, the code is as follows:

using UnityEngine;

namespace ARPGFinish.Character
{
    
    
    /// <summary>
    /// 角色动画系统
    /// </summary>
    public class CharacterAnimation : MonoBehaviour
    {
    
    
        /// <summary>
        /// 动画组件
        /// </summary>
        private Animator anim;

        /// <summary>
        /// 上一个播放的动画
        /// </summary>
        string preAnimName = "idle";

        private void Start()
        {
    
    
            anim = GetComponentInChildren<Animator>();
        }

        /// <summary>
        /// 播放动画
        /// 结束上一个动画,播放当前动画
        /// </summary>
        /// <param name="animName">需要播放的动画名称</param>
        public void PlayAnimation(string animName)
        {
    
    
            Debug.Log("========" + animName);
            // 结束上一个动画
            anim.SetBool(preAnimName, false);
            // 开始新的动画
            anim.SetBool(animName, true);
            // 将当前动画设为上一个动画
            preAnimName = animName;
        }
    }
}

Drag the new script into Player.
insert image description here

operation result

Please add a picture description

About Scenes and Modeling

Since this chapter mainly explains the animation development part of Unity, the work related to modeling and animation is not mentioned in this chapter. If necessary, you can consider downloading it in the Asset Store, or buying it in some model animation malls.


For more information, please check the general catalog [Unity] Unity study notes catalog arrangement

Guess you like

Origin blog.csdn.net/xiaoyaoACi/article/details/126539817