找回密码
 立即注册
查看: 229|回复: 0

[简易教程] Unity3D开发之人物的移动

[复制链接]
发表于 2023-3-9 09:01 | 显示全部楼层 |阅读模式
本文只是记录作者的学习过程!如果写的很水让你生气,那我道歉,私密马赛!!
功能点1:玩家输入模块
              主要就是为了用脚本文件获取玩家的键盘输入,这获取是移动的输入就是我们常用的WASD。
架构图:
              通过二维的输入值-1到1来完成方向的信号输入。


unity界面:



新建脚本

脚本代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playinput : MonoBehaviour
{

    //Variable
     public string KeyUp;
     public string KeyDown;
     public string KeyLeft;
     public string KeyRight;

    public float Dup;
    public float Dright;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Dup = (Input.GetKey(KeyUp) ? 1.0f : 0) - (Input.GetKey(KeyDown) ? 1.0f : 0);
        Dright = (Input.GetKey(KeyRight) ? 1.0f : 0) - (Input.GetKey(KeyLeft) ? 1.0f : 0);
    }
}将玩家的输入转变为-1到1的信号。
为了使得人物的移动更加平滑,我们需要用到Mathf.SmoothDamp()这个函数。

功能点2:配置人物模型以及动画的绑定
创建一个空的空对象,将人物模型导入,并绑定rigidbody和capsule collider


为创建模型创建Animator,创建一个new blend tree



添加参数forward,并且绑定idle和walk动画,forward参数的作用是控制人物在idle和walk之间的状态转换,但是他不是一种硬转换而是一种渐变混合的转换,比如forward为0则人物idle,forward为1人物walk,但是forward为0.5,人物是缓慢的行走类似于两种状态间的一种插值的状态。


接下来,通过脚本来获取按键信息来控制人物的行走。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playinput : MonoBehaviour
{

    //Variable
    public string KeyUp;
    public string KeyDown;
    public string KeyLeft;
    public string KeyRight;

    public float Dup;
    public float Dright;
    public float Dmag;
    public Vector3 Dvec;



    public bool inputEnabled = true;
    private float targetDup;
    private float targetDright;
    private float velocityDup;
    private float velocityDright;

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

    // Update is called once per frame
    void Update()
    {
        targetDup = (Input.GetKey(KeyUp) ? 1.0f : 0) - (Input.GetKey(KeyDown) ? 1.0f : 0);
        targetDright = (Input.GetKey(KeyRight) ? 1.0f : 0) - (Input.GetKey(KeyLeft) ? 1.0f : 0);

        if (inputEnabled == false){
            targetDright = 0;
            targetDup = 0;
        }
        Dup = Mathf.SmoothDamp(Dup, targetDup, ref velocityDup,0.1f);
        Dright = Mathf.SmoothDamp(Dright, targetDright, ref velocityDright, 0.1f);
        Dmag = Mathf.Sqrt((Dup * Dup) + (Dright * Dright));
        Dvec = Dright * transform.right + Dup * transform.forward;
    }
}
具体来解释一下如何控制forward值,假如我们用的是一台主机游戏ps5,我们用手柄来控制人物的行走,如下图,我们用推杆距离推杆中心的距离来设置forward的值,如果推杆在坐标(0,1)上那么forward就是1。


Dmag = Mathf.Sqrt((Dup * Dup)+(Dright * Dright));//控制人物的动画状态的转换,之后我们会将Dmag传给forward。
Dvec = Dright * transform.right + Dup * transform.forward;//计算人物移动的距离

我们在模型所在目录上面新建了一个脚本文件。


脚本代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActorController : MonoBehaviour
{

    public GameObject model;
    public playinput pi;
    public float walkSpeed = 1.4f;

    [SerializeField]
    private Animator anim;
    private Rigidbody rigid;
    private Vector3 movingVec;
   
    // Start is called before the first frame update
    void Awake()
    {
        pi = GetComponent<playinput>();
        anim = model.GetComponent<Animator>();
        rigid = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        anim.SetFloat("forward", pi.Dmag );//将Dmg传给forward
        //推杆没有移动则人物不移动
         if(pi.Dmag > 0.1f)
        {
            model.transform.forward = pi.Dvec;//将移动的方向传给模型
        }

        movingVec = pi.Dmag * model.transform.forward * walkSpeed;//移动的方向,距离,人物的动画与推杆距离位置中心的距离
                                                                  //模型的朝向有关,walkspeed是为了适配让walk适配动画
        
    }

    private void FixedUpdate()
    {
        //rigid.position += movingVec * Time.fixedDeltaTime;
        rigid.velocity = new Vector3(movingVec.x,rigid.velocity.y, movingVec.z);
    }
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-30 05:07 , Processed in 0.118993 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表