Bộ điều khiển FPS thống nhất

FPS (hoặc Bắn súng góc nhìn thứ nhất) là trò chơi trong đó nhân vật chính được điều khiển từ góc nhìn thứ nhất.

Các điều khiển thông thường là W, A, S và D để đi bộ, Nhìn chuột để nhìn xung quanh, Space để nhảy và Shift trái để chạy nước rút, cho phép người chơi di chuyển tự do quanh cấp độ.

Trong bài đăng này, tôi sẽ hướng dẫn cách tạo bộ điều khiển FPS trong Unity để xử lý việc xoay camera và chuyển động của người chơi.

bước

Để tạo bộ điều khiển FPS, hãy làm theo các bước dưới đây:

  • Tạo một Game Object mới (GameObject -> Create Empty) và đặt tên cho nó "FPSPlayer"
  • Tạo một Capsule mới (GameObject -> Đối tượng 3D -> Capsule) và di chuyển nó vào trong Đối tượng "FPSPlayer"
  • Xóa thành phần Capsule Collider khỏi Capsule và thay đổi vị trí của nó thành (0, 1, 0)
  • Di chuyển Camera chính bên trong Đối tượng "FPSPlayer" và thay đổi vị trí của nó thành (0, 1.64, 0)

  • Tạo một tập lệnh mới, đặt tên là "SC_FPSController" và dán đoạn mã bên dưới vào trong tập lệnh đó:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]

public class SC_FPSController : MonoBehaviour
{
    public float walkingSpeed = 7.5f;
    public float runningSpeed = 11.5f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Camera playerCamera;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 45.0f;

    CharacterController characterController;
    Vector3 moveDirection = Vector3.zero;
    float rotationX = 0;

    [HideInInspector]
    public bool canMove = true;

    void Start()
    {
        characterController = GetComponent<CharacterController>();

        // Lock cursor
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        // We are grounded, so recalculate move direction based on axes
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 right = transform.TransformDirection(Vector3.right);
        // Press Left Shift to run
        bool isRunning = Input.GetKey(KeyCode.LeftShift);
        float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
        float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
        float movementDirectionY = moveDirection.y;
        moveDirection = (forward * curSpeedX) + (right * curSpeedY);

        if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
        {
            moveDirection.y = jumpSpeed;
        }
        else
        {
            moveDirection.y = movementDirectionY;
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        if (!characterController.isGrounded)
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);

        // Player and Camera rotation
        if (canMove)
        {
            rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
            rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
            playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
            transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
        }
    }
}
  • Đính kèm tập lệnh SC_FPSController vào Đối tượng "FPSPlayer" (Bạn sẽ nhận thấy rằng nó cũng đã thêm một thành phần khác gọi là Bộ điều khiển ký tự, thay đổi giá trị trung tâm của nó thành (0, 1, 0))
  • Gán Camera chính cho biến Player Camera trong SC_FPSController

Bộ điều khiển FPS thống nhất

Bộ điều khiển FPS hiện đã sẵn sàng!

Nguồn
📁FPSController.unitypackage53.91 KB
Bài viết được đề xuất
Thêm Cúi người vào Trình phát FPS trong Unity
Hướng dẫn nhảy tường 3D và 2D của người chơi cho Unity
Unity Cách tạo điều khiển cảm ứng trên thiết bị di động
Bộ điều khiển nhân vật Cách thêm khả năng đẩy các vật cứng trong Unity
Tạo chuyển động của người chơi trong Unity
Cách thêm hỗ trợ nền tảng di chuyển vào bộ điều khiển nhân vật trong Unity
Bộ điều khiển trình phát hành tinh dựa trên thân cứng cho Unity