3인칭 시점의 카메라를 구현할 것이다.
카메라는 플레이어의 마우스 이동에 따라 회전하며, 다양한 각도에서 플레이어 캐릭터를 비춘다.
이 방법은 회전값을 이용해 카메라의 위치를 계산한 뒤, LookAt으로 시점을 고정하는 방식을 사용한다.
완성본

3인칭 시점 카메라 전체 코드
using Unity.Hierarchy;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float sensitivityX = 300;
[SerializeField] float sensitivityY = 200;
[SerializeField] float maxRotationY = 60;
[SerializeField] float minRotationY = -30;
[SerializeField] float distance = 10;
private float yaw;
private float pitch;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
pitch -= Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
pitch = Mathf.Clamp(pitch, minRotationY, maxRotationY);
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0);
Vector3 offset = rotation * new Vector3( 0,0 ,-distance);
transform.position = target.position + offset;
transform.LookAt(target);
}
}
코드 설명
변수 선언
[SerializeField] Transform target;
[SerializeField] float sensitivityX = 300;
[SerializeField] float sensitivityY = 200;
[SerializeField] float maxRotationY = 60;
[SerializeField] float minRotationY = -30;
[SerializeField] float distance = 10;
private float yaw;
private float pitch;
target
플레이어 캐릭터, 즉 카메라가 바라볼 오브젝트이다.
sensitivityX , sensitivityY
카메라 감도의 민감성을 곱해주는 값들이다.
값이 클수록 카메라가 더 빠르게 움직인다.
maxRotationY , min RotationY
카메라가 위 아래로 바라보는 방향의 최솟값과 최댓값이다.
minRotationY는 카메라가 아래에서 바라보는 최솟값이고,
maxRotationY는 카메라가 위에서 바라보는 최댓값이다.
distance
카메라와 target의 거리이다.
값이 클수록 카메라와 target의 거리가 멀어진다.
yaw , pitch
마우스 이동에 따른 카메라의 회전값을 넣는 변수이다.
yaw는 Y축 회전값 (Mouse X)를 이용해 계산한다.
pitch는 X축 회전값 (Mouse Y)를 이용해 계산한다.
마우스 커서 코드
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
Cursor.lockState를 Locked모드로 하여 마우스 커서가 화면 가운데에 고정되도록 한다.
Cursor.visible를 false로 하여 마우스 커서가 화면에 보이지 않게 한다.
카메라 움직임 전체 코드
void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
pitch -= Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
pitch = Mathf.Clamp(pitch, minRotationY, maxRotationY);
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0);
Vector3 offset = rotation * new Vector3( 0,0 ,-distance);
transform.position = target.position + offset;
transform.LookAt(target);
}
LateUpdate에서 호출하는 이유
3인칭 카메라의 경우 플레이어를 따라가는 역할이므로,
플레이어의 이동이 끝이 난 후 카메라 움직임을 처리하는 것이 안정적이다.
LateUpdate는 모든 Update가 끝난뒤 호출되므로, 호출되는 순서에 따라 발생할 수 있는 카메라 떨림을 방지할 수 있다.
마우스 이동값 넣기
yaw += Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
pitch -= Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
yaw와 pitch에 마우스 이동값에 민감도를 각각 곱하여 더해준다.
Input.GetAxis("Mouse X")
프레임동안 마우스의 이동량을 반환한다.
화면 왼쪽으로 마우스가 이동하면 음수를
화면 오른쪽으로 마우스가 이동하면 양수를 반환한다. Y축도 마찬가지
(값의 민감도는 설정할 수 있다.)
Mouse X, Mouse Y의 경우 GetAxis와 GetAxisRaw의 차이는 없다.
pitch에 마이너스를 하는 이유
pitch의 경우 스크린 화면 기준 마우스 좌표의 y축값을 받는데,
사람들은 보통 마우스를 올렸을 때, 카메라는 아래쪽으로 향하고
내렸을 때는 카메라가 위쪽으로 향하게끔 생각하기 때문에,
Input.GetAxis("Mouse Y")의 값이 양수 즉, 마우스를 올리면 pitch의 값을 내린다.
pitch 최솟값 최댓값
pitch = Mathf.Clamp(pitch, minRotationY, maxRotationY);
Mathf.Clmap를 사용하여 pitch의 최솟값과 최댓값을 정해준다.
minRotationY를 통해 카메라가 아래쪽으로 극심하게 회전하지 않게 하고,
maxRotationY를 통해 카메라가 위쪽으로 극심하게 회전하지 않게 한다.
회전값 만들기
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0);
계산한 각도들을 3D공간에서 사용위해 Quaternion.Euler를 사용하여 Quaternion으로 바꿔준다.
Quaternion.Euler는 Euelr각도 (pitch, yaw, roll)를 Quaternion 형으로 반환해주는 함수이다.
카메라의 위치 잡기
Vector3 offset = rotation * new Vector3( 0,0 ,-distance);
회전값과 방향 벡터를 곱하여 카메라의 추가 위치를 잡는다.
Quaternion * Vector3 연산
Vector3에서 Quaternion만큼 방향을 회전한 값을 반환한다. (Vector3 반환)
Vector3를 한 방향을 나타내는 화살표라고 예시로 들면
Quaternion은 그 화살표에서 추가로 회전하는 구조이다.
순서에 맞게 사용해야 한다. (Vector3 * Quaternion은 오류가 난다.)
카메라 위치와 시점
transform.position = target.position + offset;
transform.LookAt(target);
카메라의 위치를 target의 위치 + 추가 위치로 잡아준다.
실제 카메라의 바라보는 방향은 LookAt(target)을 통해 항상 target을 향하도록 설정된다.
'Unity > Tutorial' 카테고리의 다른 글
| [Unity] Cinemachine (기초) (0) | 2026.02.24 |
|---|---|
| [Unity] Character Controller (3D) (0) | 2026.02.24 |
| [Unity] 1인칭 시점 카메라 (3D) (0) | 2026.02.22 |
| [Unity] 카메라 회전에 따른 플레이어 회전과 움직임 (3D) (0) | 2026.02.21 |
| [Unity] 플레이어 입력과 움직임 (3D) (0) | 2026.02.13 |