48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
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 float gravityValue = Physics.gravity.y;
|
|
|
|
private void Awake()
|
|
{
|
|
_controller = GetComponentInChildren<CharacterController>(true);
|
|
_moveProvider = GetComponentInChildren<ContinuousMoveProvider>(true);
|
|
}
|
|
|
|
public void OnSceneLoaded()
|
|
{
|
|
// 씬이 다시 로드돼도 한 번만 구독되도록 중복 제거 후 등록
|
|
InputManager.Instance.OnJump_Event -= this.OnJump;
|
|
InputManager.Instance.OnJump_Event += this.OnJump;
|
|
}
|
|
|
|
public void OnJump()
|
|
{
|
|
if (_controller == null) return;
|
|
if (!_controller.isGrounded) return;
|
|
|
|
_playerVelocity.y = Mathf.Sqrt(_jumpHeight * -2f * gravityValue);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// 바닥 체크 및 Y축 속도 초기화
|
|
if (_controller.isGrounded && _playerVelocity.y < 0)
|
|
{
|
|
// 완전히 0으로 두면 바닥 감지가 불안정할 수 있으므로 약간의 음수 값을 유지
|
|
_playerVelocity.y = -2f;
|
|
}
|
|
|
|
_playerVelocity.y += gravityValue * Time.deltaTime;
|
|
_controller.Move(_playerVelocity * Time.deltaTime);
|
|
}
|
|
}
|