Projext/Assets/Scripts/Camera/PlayerAim.cs
2026-02-02 17:30:23 +09:00

35 lines
1.5 KiB
C#

using UnityEngine;
public class PlayerAim : MonoBehaviour
{
[SerializeField] private PlayerHealth health;
[Header("회전 설정")]
[SerializeField] private float rotationSpeed = 15f; // 감도 조절
[SerializeField] private float rotationDeadzone = 1.2f; // 근접전 '뒤돌기' 방지 범위
public void RotateTowardsMouse()
{
if (health != null && health.IsDead) return;
// 1. 캐릭터의 월드 위치를 화면 상의 2D 좌표로 변환합니다.
Vector3 playerScreenPos = Camera.main.WorldToScreenPoint(transform.position);
// 2. 마우스의 화면 좌표를 가져옵니다.
Vector3 mousePos = Input.mousePosition;
// 3. 화면상에서 플레이어 -> 마우스로 향하는 2D 방향 벡터를 구합니다.
Vector3 dir = mousePos - playerScreenPos;
// 4. 거리가 너무 가까우면 회전을 무시합니다 (데드존 적용).
if (dir.magnitude < rotationDeadzone * 50f) return;
// 5. 2D 방향 벡터를 각도(Atan2)로 변환합니다.
// 화면의 X는 월드의 X, 화면의 Y는 월드의 Z에 대응합니다 (쿼터뷰/탑다운 기준).
float angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
// 6. 계산된 각도로 캐릭터를 부드럽게 회전시킵니다.
Quaternion targetRotation = Quaternion.Euler(0, angle, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}