• 【Unity】第一人称视角开发


    需求

    我的需求是在Unity构建一个第一人称视角,实现移动跳跃功能。
    主要参考的是这篇博文提供的方案,不过该方案为了只允许一次跳跃,单独在人物底部构建了一个空对象,我无需此限制,因此对其进行了简化。

    实现

    1.创建场景

    创建场景对象如下:

    • Ground 地形对象
    • Player 胶囊体视作人物
    • Cube 参照对象
      在这里插入图片描述
      将Main Camera移至于人物眼睛位置
      在这里插入图片描述

    2.添加组件

    给人物对象添加Character Controller
    在这里插入图片描述

    3.绑定脚本

    创建CameraController,绑定到Main Camera

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CameraController : MonoBehaviour
    {
        //获得player的transform
        public Transform player;
        //获取鼠标移动的值
        private float mouseX, mouseY;
        //添加鼠标灵敏度
        public float mouseSensitivity;
        //声明变量来累加mouseY
        public float xRotation;
    
        void Update()
        {
            //获得鼠标左右移动的值
            mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
            //获得鼠标上下移动的值
            mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
           
    
            xRotation -= mouseY;
            //用数学函数Mathf.Clamp()将xRotation的值限制在一个范围内
            xRotation = Mathf.Clamp(xRotation, -70, 70);
    
            //使用transform中的Rotate()方法使player旋转
            player.Rotate(Vector3.up * mouseX);
            //使用transform.localRotation()方法使相机上下旋转
            transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    创建PlayerControaller,绑定到Player

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerControaller : MonoBehaviour
    {
    
        //获得Player的CharacterController组件
        private CharacterController cc;
        [Header("移动参数")]
        //定义player的移动速度
        public float moveSpeed;
        [Header("跳跃参数")]
        //定义player的跳跃速度
        public float jumpSpeed;
        //定义获得按键值的两个变量
        private float horizontalMove, verticalMove;
        //定义三位变量控制方向
        private Vector3 dir;
        //定义重力变量
        public float gravity;
        //定义y轴的加速度
        private Vector3 velocity;
        void Start()
        {
            //用GetComponent<>()方法获得CharacterController
            cc = GetComponent<CharacterController>();
            //初始化参数
            moveSpeed = 5;
            jumpSpeed = 5;
            gravity = 10;
        }
    
    
        void Update()
        {
            //用Input.GetAxis()方法获取按键左右移动的值
            horizontalMove = Input.GetAxis("Horizontal") * moveSpeed;
            //用Input.GetAxis()方法获取按键前后移动的值
            verticalMove = Input.GetAxis("Vertical") * moveSpeed;
    
            //将方向信息存储在dir中
            dir = transform.forward * verticalMove + transform.right * horizontalMove;
            //用CharacterController中的Move()方法移动Player
            cc.Move(dir * Time.deltaTime);
    
            //当键盘按空格的时候可以完成角色的跳跃
            if (Input.GetButtonDown("Jump"))
            {
                velocity.y = jumpSpeed;
            }
    
            //通过每秒减去重力的值不断下降
            velocity.y -= gravity * Time.deltaTime;
            //用CharacterController中的Move()方法移动y轴
            cc.Move(velocity * Time.deltaTime);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    4.运行测试

    完成之后,就可以运行测试。
    注意方向视角是通过获取鼠标偏移量进行设置,在点击运行之后,如果在编译过程中,鼠标进行移动,会造成视角和初始视角不一致的情况。

    在这里插入图片描述

    5.拓展:固定鼠标位置

    如果需要固定鼠标位置,可以参考如下方案(仅适用windows端)

    添加公有字段

    [HideInInspector]
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int SetCursorPos(int x, int y);
    
    • 1
    • 2
    • 3

    固定鼠标在屏幕中心:

    SetCursorPos((int)Screen.width / 2, (int)Screen.height / 2);
    
    • 1
  • 相关阅读:
    单例模式(创建型设计模式)的 C++ 代码示例模板
    Datawhale 202208 GitModel |线性规划 整数规划 多目标规划 灵敏度分析
    第5集丨Caché 对象介绍
    EtherCAT轴扩展模块EIO16084在运动控制系统中的应用
    面试专区|【32道接口API测试基础高频题整理(附答案背诵版)】
    Matlab中 * 与 .* 的区别
    PerfView专题 (第十篇):洞察 C# 终结队列引发的内存泄漏
    解决Maven打包Nacos时插件报错
    JAVA独龙族民族特色服务网站计算机毕业设计Mybatis+系统+数据库+调试部署
    Python项目运行过程报错处理
  • 原文地址:https://blog.csdn.net/qq1198768105/article/details/127766321