// 플레이어 이동 처리
void Move()
{
// 입력값을 기준으로 이동 방향 계산
Vector3 dir = transform.forward * curMovementInput.y + transform.right * curMovementInput.x;
dir *= moveSpeed;
dir.y = _rigidbody.velocity.y; // 기존 Y축 속도 유지 (중력 반영)
_rigidbody.velocity = dir; // Rigidbody의 속도 설정
}
transform.right * curMovementInput.x; 부분은 플레이어가 왼쪽으로 기동하게 되면 값이 음수가 되기 때문에 자동으로 왼쪽 이동이 처리된다. // 카메라 회전 처리
void CameraLook()
{
// 마우스 움직임을 반영해 X축(상하) 회전 값 변경
camCurXrot += mouseDelta.y * lookSensitivity;
// camCurXrot 값을 minXLook ~ maxXLook 범위로 제한
camCurXrot = Mathf.Clamp(camCurXrot, minXLook, maxXLook);
// // 카메라 컨테이너 회전 적용
// cameraContainer.localEulerAngles = new Vector3(-camCurXrot, 0, 0);
//
// // Y축(좌우) 회전 적용
// transform.localEulerAngles += new Vector3(0, mouseDelta.x * lookSensitivity, 0);
}
mouseDelta.y * lookSensitivity -> 마우스 이동량을 회전 값에 반영Mathf.Clamp(camCurXrot, minXLook, maxXLook);카메라가 360도 돌아가는 걸 방지
자연스러운 시점 제한
Mathf.Clamp를 쓰면 카메라가 일정 범위를 넘어가지 않도록 제한할 수 있고, 1인칭(FPS) 게임에서 고개가 180도 넘어가거나 화면이 뒤집히는 현상을 막아줄 수 있다. // 상태 값을 추가하는 함수 (체력 회복 등)
public void Add(float value)
{
curValue = Mathf.Min(curValue + value, maxValue); // curValue에 값을 더하지만, maxValue를 초과하지 않도록 제한
}
curValue = 90, value = 20, maxValue = 100일 때 90 + 20 = 110이 되지만, Mathf.Min(110, 100)이므로 curValue = 100 (최대 값 유지) // 상태 값을 감소시키는 함수 (데미지, 배고픔 감소 등)
public void Subtrack(float value)
{
curValue = Mathf.Max(curValue - value, 0); // curValue에서 값을 빼지만, 0 미만으로 내려가지 않도록 제한
}
curValue = 10, value = 20이면 10 - 20 = -10이지만, Mathf.Max(-10, 0)이므로 curValue = 0 (0 이하로 내려가지 않음) // 카메라 회전 처리
void CameraLook()
{
// // 마우스 움직임을 반영해 X축(상하) 회전 값 변경
// camCurXrot += mouseDelta.y * lookSensitivity;
//
// // camCurXrot 값을 minXLook ~ maxXLook 범위로 제한
// camCurXrot = Mathf.Clamp(camCurXrot, minXLook, maxXLook);
// 카메라 컨테이너 회전 적용
cameraContainer.localEulerAngles = new Vector3(-camCurXrot, 0, 0);
// Y축(좌우) 회전 적용
transform.localEulerAngles += new Vector3(0, mouseDelta.x * lookSensitivity, 0);
}
cameraContainer.localEulerAngles-camCurXrot에 -를 붙인 이유transform.localEulerAngles부모-자식 관계를 활용하기 위해
직접 회전 값을 설정하려면 오일러 각이 필요