102 lines
3.5 KiB
C#
102 lines
3.5 KiB
C#
using Unity.XR.CoreUtils;
|
|
using UnityEngine;
|
|
using UnityEngine.XR.Interaction.Toolkit.Locomotion.Movement;
|
|
|
|
public class PlayerController : MonoBehaviour, ISceneInitializable
|
|
{
|
|
[Header("점프 설정")]
|
|
[SerializeField] private float _jumpHeight = 1.2f;
|
|
|
|
private Vector3 _playerVelocity;
|
|
private CharacterController _controller;
|
|
private ContinuousMoveProvider _moveProvider;
|
|
private XROrigin _xrOrigin;
|
|
private float gravityValue = Physics.gravity.y;
|
|
|
|
public bool IsSitting { get; private set; } // 앉아있는 동안 true (이동/점프 잠금)
|
|
private float _standingHeight; // 앉기 전 카메라 높이(일어설 때 복원용)
|
|
|
|
private void Awake()
|
|
{
|
|
_controller = GetComponentInChildren<CharacterController>(true);
|
|
_moveProvider = GetComponentInChildren<ContinuousMoveProvider>(true);
|
|
_xrOrigin = GetComponentInChildren<XROrigin>(true);
|
|
}
|
|
|
|
public void OnSceneLoaded()
|
|
{
|
|
// 씬이 다시 로드돼도 한 번만 구독되도록 중복 제거 후 등록
|
|
InputManager.Instance.OnJump_Event -= this.OnJump;
|
|
InputManager.Instance.OnJump_Event += this.OnJump;
|
|
}
|
|
|
|
public void SetHeight(float height)
|
|
{
|
|
_xrOrigin.CameraYOffset = height;
|
|
}
|
|
|
|
public void OnJump()
|
|
{
|
|
if (_controller == null) return;
|
|
if (IsSitting) return; // 앉아있는 동안은 점프 금지
|
|
if (!_controller.isGrounded) return;
|
|
|
|
_playerVelocity.y = Mathf.Sqrt(_jumpHeight * -2f * gravityValue);
|
|
}
|
|
|
|
// 앉기 시작: (좌석 지정 시) 그 위치로 이동 후 카메라 높이를 낮추고 이동을 잠근다.
|
|
public void Sit(Transform sitPoint = null)
|
|
{
|
|
if (IsSitting) return;
|
|
IsSitting = true;
|
|
|
|
if (sitPoint != null) TeleportRig(sitPoint); // 좌석 위치/방향으로 순간이동
|
|
|
|
_standingHeight = _xrOrigin.CameraYOffset; // 현재 높이 기억
|
|
SetHeight(1.2f);
|
|
|
|
if (_moveProvider != null) _moveProvider.enabled = false; // 이동 잠금
|
|
}
|
|
|
|
// XR 리그(=플레이어 전체)를 target의 위치/방향으로 순간이동.
|
|
// CharacterController는 켜진 채로 transform을 직접 바꾸면 되돌려지므로 잠시 끈다.
|
|
private void TeleportRig(Transform target)
|
|
{
|
|
Transform rig = _xrOrigin != null ? _xrOrigin.transform : transform;
|
|
|
|
bool ccWasEnabled = _controller != null && _controller.enabled;
|
|
if (_controller != null) _controller.enabled = false;
|
|
|
|
// 회전은 수평(yaw)만 적용해 기울어짐 방지
|
|
rig.SetPositionAndRotation(target.position, Quaternion.Euler(0f, target.eulerAngles.y, 0f));
|
|
|
|
if (_controller != null) _controller.enabled = ccWasEnabled;
|
|
|
|
_playerVelocity = Vector3.zero; // 텔레포트 직후 누적 낙하속도 튐 방지
|
|
}
|
|
|
|
// 일어서기: 높이 복원 + 이동 잠금 해제.
|
|
public void StandUp()
|
|
{
|
|
if (!IsSitting) return;
|
|
IsSitting = false;
|
|
|
|
SetHeight(_standingHeight);
|
|
|
|
if (_moveProvider != null) _moveProvider.enabled = true; // 이동 잠금 해제
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// 바닥 체크 및 Y축 속도 초기화
|
|
if (_controller.isGrounded && _playerVelocity.y < 0)
|
|
{
|
|
// 완전히 0으로 두면 바닥 감지가 불안정할 수 있으므로 약간의 음수 값을 유지
|
|
_playerVelocity.y = -2f;
|
|
}
|
|
|
|
_playerVelocity.y += gravityValue * Time.deltaTime;
|
|
_controller.Move(_playerVelocity * Time.deltaTime);
|
|
}
|
|
}
|