Projext/Assets/02_Scripts/Camera/PlayerAim.cs

54 lines
3.1 KiB
C#
Raw Normal View History

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
/// <summary>
/// 카메라 레이 → 바닥 평면 교차 방식으로 마우스 월드 좌표를 구하고,
/// 그 방향으로 캐릭터를 부드럽게 회전시킵니다.
///
/// ⭐ Attack.GetMouseDirection()과 동일한 방식!
/// 캐릭터 회전 = 화살 방향 = 항상 일치.
/// </summary>
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
// 1. 카메라에서 마우스 위치로 레이 발사
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 레이를 쏠거에요 -> 카메라에서 마우스 방향으로
2026-01-29 06:58:38 +00:00
// 2. 플레이어 발 높이의 수평면과 레이 교차
Plane groundPlane = new Plane(Vector3.up, rotationRoot.position); // 만들거에요 -> 루트 높이의 수평면을
2026-01-29 06:58:38 +00:00
if (!groundPlane.Raycast(ray, out float distance)) return; // 중단할거에요 -> 교차 실패하면
2026-01-29 06:58:38 +00:00
// 3. 교차점 = 마우스가 가리키는 바닥 월드 좌표
Vector3 mouseWorldPos = ray.GetPoint(distance); // 계산할거에요 -> 바닥 위 마우스 월드 좌표를
2026-01-29 06:58:38 +00:00
// 4. 플레이어 → 마우스 월드 좌표 방향
Vector3 dir = mouseWorldPos - rotationRoot.position; // 계산할거에요 -> 방향 벡터를
dir.y = 0f; // 무시할거에요 -> 수직 성분을
// 5. 데드존 — 너무 가까우면 회전 무시
if (dir.sqrMagnitude < rotationDeadzone * rotationDeadzone) return; // 중단할거에요 -> 너무 가까우면
2026-01-29 06:58:38 +00:00
2026-02-25 10:39:20 +00:00
// 6. 부드럽게 회전
Quaternion targetRotation = Quaternion.LookRotation(dir); // 생성할거에요 -> 목표 회전값을
2026-02-25 10:39:20 +00:00
rotationRoot.rotation = Quaternion.Slerp(rotationRoot.rotation, targetRotation, rotationSpeed * Time.deltaTime); // 회전시킬거에요 -> 루트 오브젝트를 부드럽게
2026-01-29 06:58:38 +00:00
}
}