当前位置:首页 » 《随便一记》 » 正文

Unity笔记-09-玩家控制器_韩天衣的博客

13 人参与  2022年01月12日 13:25  分类 : 《随便一记》  评论

点击全文阅读


Unity笔记-09-玩家控制器

WASD控制人物朝向行走以及鼠标控制人物正方向

需求分析

摄像机跟随

摄像机保持在人物的后方并且相对静止,人物朝向旋转,摄像机也能够绕着人物进行旋转并保持距离不变,也就是说摄像机位于以人物为圆心的r距离圆形轨道上

鼠标移动控制摄像机镜头旋转人物正方向

鼠标移动能够控制人物的正方向,例如鼠标左移,人物朝向旋转到左,并且摄像机能够自动移动到对应位置,保持玩家视角不变

键盘控制人物朝向并能够移动

WASD可以控制人物的朝向向前,左,后,右方向并能够沿着朝向移动

功能拆解与逐步实现

实现:摄像机跟随

游戏开始时,给予摄像机初始跟随位置

获得摄像机初始偏移量,该初始偏移量将会是摄像机跟随人物的永久偏移量,模长是不变的,也就是由人物指向摄像机的方向向量,通过摄像机向量减去人物向量即可获得偏移量

Vector3 relative = camera.transform.position - transform.position;

每次人物移动,鼠标移动摄像机都必须保持跟随,因此需要将偏移量加上人物位置即为摄像机位置

camera.transform.position = transform.position + relative;

实现:鼠标控制镜头以及人物正方向

通过虚拟轴获得鼠标移动量

float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");

鼠标水平移动,控制人物朝向以及摄像机旋转

更改人物正方向以及摄像机朝向,注意这里不能加入垂直旋转,因此抬头仅仅是视角向上以及人物的对应抬头动画,人物的朝向不能有垂直方向的偏移,注意人物的正方向与摄像机朝向时时刻刻都是一致的

transform.forward = Quaternion.Euler(0, x, 0)*transform.forward;

camera.transform.forward = transform.forward;

摄像机旋转,首先摄像机的偏移量应当旋转,这是由人物指向物体的方向向量,对应旋转控制摄像机旋转后的位置

relative = Quaternion.Euler(0, x, 0) * relative; 偏移量旋转

camera.transform.position = transform.position + relative; 旋转后的偏移量加人物位置向量即为摄像机位置

实现:键盘控制人物朝向并移动

虚拟轴获得键盘按键

float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");

注意,通过虚拟轴的获得的人物移动方向向量应当以人物的坐标系为准,因此需要通过transform.TransformPoint方法将自身坐标系对应方向向量转化为世界坐标系对应向量,再将此向量减去人物位置向量,即可获得与人物坐标系对应方向向量方向一致的世界坐标系向量

Vector3 Direction = transform.TransformPoint(x, 0, y);

Direction =Direction-transform.position;

但是由于人物朝向与正方向设定的冲突,因此需要将脚本挂在人物的父空物体上,通过父空物体的移动来控制人物移动,人物朝向则另外控制,防止冲突。

控制父空物体移动

transform.Translate(x*Time.deltaTime, 0, y*Time.deltaTime);

调整人物对应朝向

Enemy.forward=Direction;

播放移动动画

鼠标隐藏与位置锁定

按下Command鼠标显示,解除锁定,其余情况鼠标隐藏,并锁定在屏幕中央

if (!Input.GetKey(KeyCode.LeftCommand))
{
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
}
else
{
    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.None;
}

完整代码

public class PlayerControl : MonoBehaviour
{
    /// <summary>
    /// 摄像机
    /// </summary>
    private Camera camera;
    /// <summary>
    /// 初始偏移量
    /// </summary>
    private Vector3 relative;
    /// <summary>
    /// 动画组件
    /// </summary>
    private Animation anim;
    /// <summary>
    /// 人物组件
    /// </summary>
    private Transform Enemy;
    /// <summary>
    /// 初始化
    /// </summary>
    private void Start()
    {
        Enemy = transform.GetChild(0);
        anim = transform.GetChild(0).GetChild(0).GetComponent<Animation>();
        camera = GameObject.FindObjectOfType<Camera>();
        //初始偏移量初始化使用初始设定好的位置方便调试
        relative = camera.transform.position - transform.position;
    }
    /// <summary>
    /// 摄像机跟随
    /// </summary>
    private void CameraFollow()
    {
        camera.transform.position = transform.position + relative;
    }
    /// <summary>
    /// 鼠标控制
    /// </summary>
    private void MouseControlForward()
    {
        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");
        if (x != 0 || y != 0)
        {
            transform.forward = Quaternion.Euler(0, x, 0)*transform.forward;
            camera.transform.forward = transform.forward;
            relative = Quaternion.Euler(0, x, 0) * relative;
            camera.transform.position = transform.position + relative;
        }
        else
        {
            CameraFollow();
        }
    }
    /// <summary>
    /// 鼠标隐藏
    /// </summary>
    private void MouseHide()
    {
        if (!Input.GetKey(KeyCode.LeftCommand))
        {
            Cursor.visible = false;
            Cursor.lockState = CursorLockMode.Locked;
        }
        else
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }
    }
    /// <summary>
    /// 人物移动
    /// </summary>
    private void move()
    {
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
        {
            float x = Input.GetAxis("Horizontal");
            float y = Input.GetAxis("Vertical");
            Vector3 Direction = transform.TransformPoint(x, 0, y);
            Direction =Direction-transform.position;
            transform.Translate(x*Time.deltaTime, 0, y*Time.deltaTime);
            Enemy.forward=Direction;
            anim.Play("EnemyRun");
        }
        else
        {
            anim.Stop("EnemyRun");
        }
    }
    /// <summary>
    /// 渲染更新
    /// </summary>
    private void Update()
    {
        move();
        MouseControlForward();
        MouseHide();
    }
}


点击全文阅读


本文链接:http://zhangshiyu.com/post/33107.html

人物  摄像机  鼠标  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1