【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);
    }

}

创建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);
    }
}

4.运行测试

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

在这里插入图片描述

5.拓展:固定鼠标位置

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

添加公有字段

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

固定鼠标在屏幕中心:

SetCursorPos((int)Screen.width / 2, (int)Screen.height / 2);

猜你喜欢

转载自blog.csdn.net/qq1198768105/article/details/127766321
今日推荐