【Unityスクール】FPSプレイヤーコントローラーの作成法

こんにちは。
今回は簡単に誰でもできるUnityでFPSのプレイヤーコントローラーを作る方法を紹介します。
一番上に空の親オブジェクトを用意。
その下の子オブジェクトにカメラとプレイヤーのモデルオブジェクトを配置。
カメラの子オブジェクトにガン(銃)のモデルオブジェクトを配置します。
プレイヤーインスペクター
Add Component を押してSphere ColliderとRigidBodyを追加します。上の例だと、Scale値が20と大きめですが、任意の大きさで大丈夫です。
Sphere Colliderの Is Triggerはチェック外す。Rigidbody のUse Gravityチェック外す。ConstraintsはFreeze Rotationのみ全てチェック(ローテーションの固定)を入れます。
後ほど紹介する2つのスクリプトを新規作成しプレイヤーオブジェクトにアタッチしてください。
C#スクリプト
- Player Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
using UnityEngine; [RequireComponent(typeof(PlayerMotor))] public class PlayerController : MonoBehaviour { [SerializeField] private float speed = 5f; [SerializeField] private float lookSensitivity = 3f; private PlayerMotor motor; void Start() { motor = GetComponent<playermotor>(); } void Update() { //Calculate movement velocity as a 3D vector float _xMov = Input.GetAxisRaw("Horizontal"); float _zMov = Input.GetAxisRaw("Vertical"); Vector3 _movHorizontal = transform.right * _xMov; Vector3 _movVertical = transform.forward * _zMov; //Final movement vector Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed; //Apply movement motor.Move(_velocity); //Calculate rotation as a 3D vector(turning around) float _yRot = Input.GetAxis("Mouse X"); Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity; //Apply rotation motor.Rotate(_rotation); //Calculate rotation as a 3D vector(turning around) float _xRot = Input.GetAxis("Mouse Y"); float _cameraRotationX = _xRot * lookSensitivity; //Apply rotation motor.RotationCamera(_cameraRotationX); } } |
- Player Motor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class PlayerMotor : MonoBehaviour { [SerializeField] private Camera cam; private Vector3 velocity = Vector3.zero; private Vector3 rotation = Vector3.zero; private float cameraRotationX = 0f; private float currentCameraRotationX = 0f; [SerializeField] private float cameraRotationLimit = 85; private Rigidbody rb; private void Start() { rb = GetComponent<rigidbody>(); } //Gets a movement Vector public void Move(Vector3 _velocity) { velocity = _velocity; } //Gets a rotational Vector public void Rotate(Vector3 _rotation) { rotation = _rotation; } //Gets a rotational Vector for the camera public void RotationCamera(float _cameraRotationX) { cameraRotationX = _cameraRotationX; } private void FixedUpdate() { PerformMovement(); PerformRotation(); } void PerformMovement() { if(velocity != Vector3.zero) { rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime); } } //Perform rotation void PerformRotation() { rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation)); if(cam != null) { //Set our rotation and clamp it currentCameraRotationX -= cameraRotationX; currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit, cameraRotationLimit); //Apply our rotation to the transfom of our camera cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f); } } } |
PlayerMotorのCamには子オブジェクトのカメラを入れてください。その他speed(プレイヤー移動速度)、lookSensitivity(カメラローテーション感度)、cameraRotationLimit(カメラ角度制限)はお好みの値でで調整してください。
以上簡単な忘備録でした。
人気記事:Unityは独学で勉強できる?
おすすめのUnityスクール
Unityが学べるおすすめのプログラミングスクールはTECH STUDIAMになります。
理由はゲーム開発に特化したプログラミングスクールであり、2ヶ月で9万円の授業料で他のプログラミングスクールに比べても安いからです。
興味のある方は、無料相談してみるのをオススメます。
ゲーム制作を学べるオンラインスクール TECH STUDIAM