Projext/Assets/Scripts/Camera/PlayerAim.cs

43 lines
2.6 KiB
C#
Raw Permalink Normal View History

2026-02-12 15:23:25 +00:00
using UnityEngine; // 유니티 엔진의 기본 기능을 불러올거에요 -> UnityEngine을
2026-01-29 06:58:38 +00:00
2026-02-12 15:23:25 +00:00
public class PlayerAim : MonoBehaviour // 클래스를 선언할거에요 -> MonoBehaviour를 상속받는 PlayerAim을
2026-01-29 06:58:38 +00:00
{
2026-02-12 15:23:25 +00:00
[SerializeField] private PlayerHealth health; // 변수를 선언할거에요 -> 플레이어 체력 스크립트를
2026-01-29 06:58:38 +00:00
2026-02-25 10:39:20 +00:00
[Header("회전 설정")] // 인스펙터 창에 제목을 표시할거에요 -> 회전 설정을
[SerializeField] private float rotationSpeed = 15f; // 변수를 선언할거에요 -> 회전 속도를
[SerializeField] private float rotationDeadzone = 1.2f; // 변수를 선언할거에요 -> 회전 무시 범위를
[Tooltip("회전 기준이 될 루트 Transform. 비워두면 이 오브젝트의 transform을 사용")]
[SerializeField] private Transform rotationRoot; // 변수를 선언할거에요 -> 회전 중심 기준 Transform을 (비워두면 자기 자신)
private void Awake() // 함수를 실행할거에요 -> 초기화를
{
// rotationRoot 미설정 시 자기 자신 사용
if (rotationRoot == null) rotationRoot = transform; // 설정할거에요 -> 기본값으로 자기 transform을
}
2026-01-29 06:58:38 +00:00
2026-02-12 15:23:25 +00:00
public void RotateTowardsMouse() // 함수를 선언할거에요 -> 마우스 방향으로 회전하는 RotateTowardsMouse를
2026-01-29 06:58:38 +00:00
{
2026-02-25 10:39:20 +00:00
if (health != null && health.IsDead) return; // 중단할거에요 -> 죽었으면
2026-01-29 06:58:38 +00:00
2026-02-25 10:39:20 +00:00
// 1. 회전 기준 오브젝트의 월드 위치를 화면 좌표로 변환
Vector3 playerScreenPos = Camera.main.WorldToScreenPoint(rotationRoot.position); // 변환할거에요 -> 루트 위치를 화면 좌표로
2026-01-29 06:58:38 +00:00
2026-02-25 10:39:20 +00:00
// 2. 마우스 화면 좌표
Vector3 mousePos = Input.mousePosition; // 가져올거에요 -> 마우스 위치를
2026-01-29 06:58:38 +00:00
2026-02-25 10:39:20 +00:00
// 3. 플레이어 → 마우스 방향 벡터
Vector3 dir = mousePos - playerScreenPos; // 계산할거에요 -> 방향 벡터를
2026-01-29 06:58:38 +00:00
2026-02-25 10:39:20 +00:00
// 4. 데드존 — 너무 가까우면 회전 무시
if (dir.magnitude < rotationDeadzone * 50f) return; // 중단할거에요 -> 마우스가 너무 가까우면
2026-01-29 06:58:38 +00:00
2026-02-25 10:39:20 +00:00
// 5. 각도 계산 (탑다운/쿼터뷰: 화면 X → 월드 X, 화면 Y → 월드 Z)
float angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg; // 계산할거에요 -> 벡터를 각도로 변환을
2026-01-29 06:58:38 +00:00
2026-02-25 10:39:20 +00:00
// 6. 부드럽게 회전
Quaternion targetRotation = Quaternion.Euler(0, angle, 0); // 생성할거에요 -> 목표 회전값을
rotationRoot.rotation = Quaternion.Slerp(rotationRoot.rotation, targetRotation, rotationSpeed * Time.deltaTime); // 회전시킬거에요 -> 루트 오브젝트를 부드럽게
2026-01-29 06:58:38 +00:00
}
}