Unity 使用按键控制角色运动

创建角色

创建一个脚本PlayerController

创建控制器,使用boolean值

 

 

 

 脚本

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

public class PlayerController : MonoBehaviour
{
    //向量
    Vector3 dir = new Vector3();
    //horizontal上一帧变量
    float horizontalDirection = 0f;
    //vertical上一帧变量
    float verticalDirection = 0f;
    //向量x值
    float x = 0f;
    //向量z值
    float z = 0f;
    //转向时旋转速度大小
    float reduce = 0.1f;
    //按键时 向量x的值
    float xDirection = 0f;
    //按键时 向量z的值
    float zDirection = 0f;

    
    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    //判断horizontal是否处在按下阶段
    bool isHorizontalMotion(float horizontal){
        if ((horizontalDirection >= 0f && ((horizontalDirection < horizontal) || horizontal < 0))  ||
        (horizontalDirection <= 0f && ((horizontalDirection > horizontal) || horizontal > 0)) ||
        (horizontal == 1.00f || horizontal == -1.00f))
        {
            return true;
        }else{
            return false;
        }
    }

    //判断vertical是否处在按下阶段
    bool isVerticalMotion(float vertical){
        if ((verticalDirection >= 0f && ((verticalDirection < vertical) || vertical < 0))  ||
        (verticalDirection <= 0f && ((verticalDirection > vertical) || vertical > 0)) ||
        (vertical == 1.00f || vertical == -1.00f))
        {
            return true;
        }else{
            return false;
        }
    }

    //horizontal进行旋转
    void horizontalRotate(float horizontal, float vertical){
        z = dir[2];
        xDirection = dir[0];
        if (vertical == 0f)
        {
            if (z < reduce && z > -reduce)
            {
                z = 0f;
            }
            if (z > 0f)
            {
                z = z - reduce;
            }
            if (z < 0f)
            {
                z = z + reduce;
            }
        }
        if (horizontal > 0)
        {
            xDirection = xDirection + reduce;
        }
        if (horizontal < 0)
        {
            xDirection = xDirection - reduce;
        }
        if (xDirection > 1.00f)
        {
            xDirection = 1.00f;
        }else if(xDirection < -1.00f){
            xDirection = -1.00f;
        }
        dir.Set(xDirection, 0, z);
    }

    //vertical进行旋转
    void verticalRotate(float horizontal, float vertical){
        x = dir[0];
        zDirection = dir[2];
        if (horizontal == 0f)
        {
            if (x < reduce && x > -reduce)
            {
                x = 0f;
            }
            if (x > 0f)
            {
                x = x - reduce;
            }
            if (x < 0f)
            {
                x = x + reduce;
            }
        }
        if (vertical > 0)
        {
            zDirection = zDirection + reduce;
        }
        if (vertical < 0)
        {
            zDirection = zDirection - reduce;
        }
        if (zDirection > 1.00f)
        {
            zDirection = 1.00f;
        }else if(zDirection < -1.00f){
            zDirection = -1.00f;
        }
        dir.Set(x, 0, zDirection);
    }

    //跑步
    void run(){
        //播放跑步动画
        animator.SetBool("isRun", true);
        //朝向前方移动
        transform.Translate(Vector3.forward * 2 * Time.deltaTime);
        
    }

    private Animator animator;
    // Update is called once per frame
    void Update()
    {

        //水平轴
        float horizontal = Input.GetAxis("Horizontal");
        //垂直轴
        float vertical = Input.GetAxis("Vertical");

        //播放跑步动画
        animator.SetBool("isRun", false);
        if (isHorizontalMotion(horizontal))
        {
            //按键时 x轴旋转
            horizontalRotate(horizontal, vertical);
            run();
        }
        if (isVerticalMotion(vertical))
        {
            //按键时 y轴旋转
            verticalRotate(horizontal, vertical);
            if(!isHorizontalMotion(horizontal))
            {
                run();
            }
        }
        
        horizontalDirection = horizontal;
        verticalDirection = vertical;

        //面向向量
        transform.rotation = Quaternion.LookRotation(dir);
    }

}

然后我们运行时还是会感觉有一点过渡衔接不自然,我们可以更改动画器参数里的设置

 

 当然设为0的话是没有过渡的动画,类似于硬直,所以按照自己的意愿来调试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值