5
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TPSカメラの作り方

Last updated at Posted at 2018-07-17
スクリーンショット 2018-07-17 16.37.55.png

空のオブジェクトを作成し、プレイヤー位置を常に追従させ、子オブジェクトにカメラを配置します。
カメラはプレイヤーから離れた位置へ(好みの場所で)。
また、Mathf.Clampで、縦方向の回転角度を制限しています。

【参考】Unityでカメラの向きを基準に移動する方法と、追従して回転できるカメラの実装

TpsCamera.cs
using UnityEngine;

public class TpsCamera : MonoBehaviour {

    [SerializeField] Transform Player;
    [SerializeField]float RotateSpeed;

    float yaw,pitch;
    
    private void Start()
    {
        RotateSpeed = 1;
    }

    void Update () {
        
        //プライヤー位置を追従する
        transform.position = new Vector3(Player.position.x, transform.position.y, Player.position.z); 

        yaw +=  Input.GetAxis("Mouse X")*RotateSpeed; //横回転入力
        pitch-=Input.GetAxis("Mouse Y")*RotateSpeed; //縦回転入力

        pitch = Mathf.Clamp(pitch, -80, 60); //縦回転角度制限する

        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f); //回転の実行
	}
}
5
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?