Unity/3d/FPS
提供: 初心者エンジニアの簡易メモ
FPS視点カメラ
- ヒエラルキーからCreateEmptyして新規オブジェクトを作成しオブジェクト名を"Player"とする
- MainCameraのオブジェクト名を"FSPCamera"へ変更
- Playerの下にFSPCameraを移動
- 以下csを作成してFPSCameraへドラッグ
FPSCamera.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FPS
{
public class FPSCamera : MonoBehaviour
{
private Transform _camTransform;
//マウス操作の始点
private Vector3 _startMousePos;
//カメラ回転の始点情報
private Vector3 _presentCamRotation;
void Start()
{
_camTransform = this.gameObject.transform;
}
void Update()
{
//カメラの回転 マウス
CameraRotationMouseControl();
}
//カメラの回転 マウス
private void CameraRotationMouseControl()
{
if (Input.GetMouseButtonDown(0))
{
_startMousePos = Input.mousePosition;
_presentCamRotation.x = _camTransform.transform.eulerAngles.x;
_presentCamRotation.y = _camTransform.transform.eulerAngles.y;
}
if (Input.GetMouseButton(0))
{
//(移動開始座標 - マウスの現在座標) / 解像度 で正規化
float x = (_startMousePos.x - Input.mousePosition.x) / Screen.width;
float y = (_startMousePos.y - Input.mousePosition.y) / Screen.height;
//回転開始角度 + マウスの変化量 * 90
float eulerX = _presentCamRotation.x + y * 90.0f;
float eulerY = _presentCamRotation.y + x * 90.0f;
_camTransform.rotation = Quaternion.Euler(eulerX, eulerY, 0);
}
}
}
}
参考:https://qiita.com/Nekomasu/items/0a6aaf1f595cf05fbd0d
参考:https://xr-hub.com/archives/7782
銃オブジェクト追加
- AssetStoreで銃(例:"FPS-AKM"で出てくる)を追加
- 銃をヒエラルキーに追加し、オブジェクト名を"Gun"に変更する
- FPSCameraの子にGunを移動
参考:https://xr-hub.com/archives/7782
プレイヤーの移動
- RigidbodyをPlayerに追加
- 下に落ちてしまう場合は、床をPlaneで作り、床にAddComponentでBoxColliderを追加し、PlayerにもCapsuleColliderを追加する
- 以下のようなPlayerController.csを作る
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody m_Rigidbody;
void Start()
{
// 自分のRigidbodyを取ってくる
m_Rigidbody = GetComponent<Rigidbody>();
}
void Update()
{
// 十字キーで首を左右に回す
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(new Vector3(0.0f, 1.0f, 0.0f));
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(new Vector3(0.0f, -1.0f, 0.0f));
}
// WASDで移動する
float x = 0.0f;
float z = 0.0f;
if (Input.GetKey(KeyCode.D))
{
x += 1.0f;
}
if (Input.GetKey(KeyCode.A))
{
x -= 1.0f;
}
if (Input.GetKey(KeyCode.W))
{
z += 1.0f;
}
if (Input.GetKey(KeyCode.S))
{
z -= 1.0f;
}
m_Rigidbody.velocity = new Vector3(x, 0.0f, z);
}
}
