Unity第一人称控制器

不得不说,unity最新的轻量级角色控制器搭配新输入系统确实好用。不过我不会(doge)。想学习的小伙伴可以直接在Hub里面下载unity的第一人称控制器模板,个人觉得非常适合学习。

简单的第一人称控制器需要解决的事情无非三件:

角色的移动,相机的旋转,重力的模拟。

对于相机的旋转,我们希望在x轴上它应该是可以实现360度的,因为角色身体的旋转在x轴是随意的,但是在上下方向也就是y轴上,我们并不希望跟随视角的转动角色可以从将它的头反转过来,比较合理的处理是限制在-90度到90度之间。以下是相机代码部分:

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

public class CameraFallow : MonoBehaviour
{

    public float mouseSensitivity = 100f;

    public Transform player;

    private float xRotation = 0f;
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked; //在游戏中隐藏鼠标
    }

    // Update is called once per frame
    void Update()
    {
        MoveMent();
    }

    void MoveMent()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        player.Rotate(Vector3.up * mouseX);

    }
}

代码的话没有什么难点,都是可以直接的处理。

再来说到角色的移动,跟重力的模拟。

在移动时需要注意的是,我们不应该让角色的移动基于整个世界坐标系,第一人称我们想要实现的效果应该是当我们角色面朝某个方向,移动的前方就应该朝向这个方向。而对于重力的模拟,简单两个物理公式以及地面检测就可以轻松实现我们的功能。

btw,尽管我们熟知重力加速度gravity是-9.81,但其实在游戏中跳跃整个过程的实现往往是很短的时间内就可以实现的,所以我们应该是这个值比原有的重力加速度大1.5倍到2倍之间,这种手感是更加舒适的。

以下是角色的代码部分:

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

public class PlayerMovement : MonoBehaviour
{
    [Header("移动速度")]
    public float speed=12f;

    [Header("重力加速度")]
    public float gravity = -9.81f;

    [Header("地面检测")]
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask GroundMask;

    [Header("跳跃高度")]
    public float jumpHeight = 3f;

    private CharacterController controller;

    private Vector3 velocity;
    private bool isGround;
   
    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    
    void Update()
    {
        movement();
    }
    void movement()
    {
        //x跟z轴移动:
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z; //根据角色的朝向进行基于x轴与z轴的移动   

        //Vector3 move =new  Vector3(x, 0f, z); //错误的赋值。
        /*这里赋值的的 vector3 是面对整个世界坐标系的,无论角色面朝任何方向,都只会在世界坐标系的x轴跟z轴上移动。*/

        controller.Move(move * speed * Time.deltaTime);


        //考虑重力的y轴移动:
        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime );

        isGround = Physics.CheckSphere(groundCheck.position, groundDistance, GroundMask);

        if (isGround && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        //跳跃
        if (Input.GetButtonDown("Jump") && isGround)
        {
            velocity.y = Mathf.Sqrt(-2 * jumpHeight * gravity); //v=sqrt(2gh);
        }
    }
}

Awesome,这次学会了简单的第一人称控制器啦,虽然相比较unity的官方模板,它的手感并不优秀,不过毕竟人家是官方嘛

猜你喜欢

转载自blog.csdn.net/qq_62440805/article/details/124880515