Unity 中的高级玩家移动

在本教程中,我们将探索 Unity 中的高级玩家移动机制,包括冲刺、蹲伏和流畅的摄像头系统。这些功能为玩家控制增添了深度和精致,增强了整体游戏体验。我们将使用 Unity 的物理系统实现逼真的移动和交互。

设置场景

在我们深入编码之前,让我们先建立一个带有玩家对象和相机的基本场景:

  1. 创建一个新的 Unity 项目。
  2. 在层次结构中,创建一个 3D 立方体,将其重命名为 Player,然后缩放它以类似于角色(例如,X:1、Y:2、Z:1)。
  3. 为播放器添加一个 Rigidbody 组件,并将 Interpolate 属性设置为 Interpolate,以实现平滑的物理效果。
  4. 添加一个 Capsule Collider 组件,调整其高度和半径以匹配玩家模型。
  5. 创建一个空的游戏对象,将其命名为 CameraRig,并将一个 Camera 附加到它。将相机放置在玩家后方略上方的位置。
  6. 使 CameraRig 成为 Player 对象的子对象,以跟随其运动。

实现高级玩家移动

我们将实现一个处理基本动作、冲刺、蹲伏和平滑摄像机旋转的脚本。

PlayerMovement 脚本

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float walkSpeed = 5f;
    public float sprintSpeed = 10f;
    public float crouchSpeed = 2.5f;
    public float jumpForce = 5f;
    public float gravity = 20f;

    public Transform cameraTransform;
    public float lookSensitivity = 2f;
    public float maxLookAngle = 80f;

    private Rigidbody rb;
    private float currentSpeed;
    private bool isCrouching = false;
    private bool isGrounded = false;

    private void Start()
    {
        rb = GetComponent();
        rb.freezeRotation = true; // Prevent the Rigidbody from rotating
    }

    private void Update()
    {
        HandleMovement();
        HandleJumping();
        HandleCrouching();
        HandleCameraRotation();
    }

    private void HandleMovement()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 move = transform.right * moveHorizontal + transform.forward * moveVertical;
        move = move.normalized * currentSpeed * Time.deltaTime;

        Vector3 velocity = rb.velocity;
        velocity.x = move.x;
        velocity.z = move.z;
        rb.velocity = velocity;
    }

    private void HandleJumping()
    {
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
        }
    }

    private void HandleCrouching()
    {
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            isCrouching = !isCrouching;
            currentSpeed = isCrouching ? crouchSpeed : walkSpeed;
            transform.localScale = new Vector3(1, isCrouching ? 0.5f : 1, 1);
        }

        if (Input.GetKey(KeyCode.LeftShift) && !isCrouching)
        {
            currentSpeed = sprintSpeed;
        }
        else if (!isCrouching)
        {
            currentSpeed = walkSpeed;
        }
    }

    private void HandleCameraRotation()
    {
        float mouseX = Input.GetAxis("Mouse X") * lookSensitivity;
        float mouseY = Input.GetAxis("Mouse Y") * lookSensitivity;

        Vector3 rotation = cameraTransform.localEulerAngles;
        rotation.y += mouseX;
        rotation.x -= mouseY;
        rotation.x = Mathf.Clamp(rotation.x, -maxLookAngle, maxLookAngle);

        cameraTransform.localEulerAngles = rotation;
    }

    private void OnCollisionStay(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
}

主要特点及说明

运动和速度控制

该脚本使用 Unity 的 Rigidbody 来移动玩家,从而实现基于物理的交互。速度会根据玩家是行走、冲刺还是蹲伏而变化。蹲伏可通过 LeftControl 键切换,而冲刺则在按住 LeftShift 键时发生。

跳跃

当玩家按下跳跃按钮(默认:空格键)时,HandleJumping 方法会施加向上的力,前提是玩家处于接地状态。地面检测通过检查与标记为 "Ground" 的物体的碰撞来处理。

相机控制

摄像机的旋转由鼠标控制,根据摄像机的位置提供第一人称或第三人称视角。视角受到限制,以防止过度倾斜,否则可能会让玩家迷失方向。

结论

本教程为 Unity 中的高级玩家移动提供了基础,涵盖了基于物理的控制、冲刺、蹲伏和摄像头管理等各个方面。这些技术可以进一步定制和扩展,以适应不同的游戏类型和风格,并通过灵敏而逼真的控制增强玩家体验。

请记住,玩家移动是游戏玩法的一个关键方面,应仔细调整以确保令人满意的玩家体验。尝试不同的值和功能,找到适合您项目的最佳设置。