Unity FPS コントローラー

FPS (一人称シューティング) は、主人公が一人称視点で操作されるゲームです。

通常のコントロールは、W、A、S、D で歩き、マウス ルックで周囲を見回し、スペースでジャンプ、左 Shift でダッシュで、プレイヤーはレベル内を自由に 移動 することができます。

この記事では、カメラの回転とプレイヤーの動きを処理する Unity で FPS コントローラーを作成する方法を説明します。

ステップ

FPS コントローラーを作成するには、次の手順に従います。

  • 新しいゲーム オブジェクトを作成し (GameObject -> Create Empty)、名前を付けます。 "FPSPlayer"
  • 新しいカプセルを作成し (ゲームオブジェクト -> 3D オブジェクト -> カプセル)、"FPSPlayer" オブジェクト内に移動します。
  • Capsule Collider コンポーネントを Capsule から削除し、その位置を (0, 1, 0) に変更します。
  • メイン カメラを "FPSPlayer" オブジェクト内に移動し、その位置を (0, 1.64, 0) に変更します。

  • 新しいスクリプト を作成し、"SC_FPSController" という名前を付けて、その中に以下のコードを貼り付けます。
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);
        }
    }
}
  • SC_FPSController スクリプトを "FPSPlayer" オブジェクトにアタッチします (キャラクター コントローラーと呼ばれる別のコンポーネントも追加されており、その中心値を (0, 1, 0) に変更していることがわかります)
  • SC_FPSController の Player Camera 変数に Main Camera を割り当てます。

Unity FPS コントローラー

これでFPSコントローラーの準備が完了しました。

ソース
📁FPSController.unitypackage53.91 KB
おすすめの記事
Unity の FPS プレーヤーにしゃがみを追加する
Unity 用プレーヤー 3D および 2D ウォール ジャンプ チュートリアル
キャラクター コントローラー Unity でリジッドボディをプッシュする機能を追加する方法
Unity モバイル タッチ コントロールの作り方
Unity でプレーヤーの動きを作成する
Unity のキャラクター コントローラーに移動プラットフォームのサポートを追加する方法
Unity 用 Rigidbody ベースの Planetary Player コントローラー