77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Colosseum.Player
|
|
{
|
|
/// <summary>
|
|
/// 3인칭 카메라 컨트롤러
|
|
/// </summary>
|
|
public class PlayerCamera : MonoBehaviour
|
|
{
|
|
[Header("Camera Settings")]
|
|
[SerializeField] private float distance = 5f;
|
|
[SerializeField] private float height = 2f;
|
|
[SerializeField] private float rotationSpeed = 2f;
|
|
[SerializeField] private float minPitch = -30f;
|
|
[SerializeField] private float maxPitch = 60f;
|
|
|
|
private Transform target;
|
|
private float yaw;
|
|
private float pitch;
|
|
private Camera cameraInstance;
|
|
private InputSystem_Actions inputActions;
|
|
|
|
public void Initialize(Transform playerTransform, InputSystem_Actions actions)
|
|
{
|
|
target = playerTransform;
|
|
inputActions = actions;
|
|
|
|
// 기존 메인 카메라 사용 또는 새로 생성
|
|
cameraInstance = Camera.main;
|
|
if (cameraInstance == null)
|
|
{
|
|
var cameraObject = new GameObject("PlayerCamera");
|
|
cameraInstance = cameraObject.AddComponent<Camera>();
|
|
cameraObject.tag = "MainCamera";
|
|
}
|
|
|
|
// 초기 각도
|
|
yaw = target.eulerAngles.y;
|
|
pitch = 20f;
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (target == null || cameraInstance == null) return;
|
|
|
|
HandleRotation();
|
|
UpdateCameraPosition();
|
|
}
|
|
|
|
private void HandleRotation()
|
|
{
|
|
if (inputActions == null) return;
|
|
|
|
// Input Actions에서 Look 입력 받기
|
|
Vector2 lookInput = inputActions.Player.Look.ReadValue<Vector2>();
|
|
float mouseX = lookInput.x * rotationSpeed * 0.1f;
|
|
float mouseY = lookInput.y * rotationSpeed * 0.1f;
|
|
|
|
yaw += mouseX;
|
|
pitch -= mouseY;
|
|
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
|
|
}
|
|
|
|
private void UpdateCameraPosition()
|
|
{
|
|
// 구면 좌표로 카메라 위치 계산
|
|
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0f);
|
|
Vector3 offset = rotation * new Vector3(0f, 0f, -distance);
|
|
offset.y += height;
|
|
|
|
cameraInstance.transform.position = target.position + offset;
|
|
cameraInstance.transform.LookAt(target.position + Vector3.up * height * 0.5f);
|
|
}
|
|
}
|
|
}
|