2026-06-17
This commit is contained in:
Binary file not shown.
@@ -1,50 +1,47 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Locomotion.Movement;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class PlayerController : MonoBehaviour,ISceneInitializable
|
||||
public class PlayerController : MonoBehaviour, ISceneInitializable
|
||||
{
|
||||
[Header("점프 설정")]
|
||||
[SerializeField] private float _jumpForce = 5f; // 점프 세기(임펄스)
|
||||
[SerializeField] private float _groundCheckDistance = 0.2f; // 바닥 판정 레이 길이
|
||||
[SerializeField] private LayerMask _groundLayer = ~0; // 바닥으로 인정할 레이어
|
||||
[SerializeField] private float _jumpHeight = 1.2f;
|
||||
|
||||
private Rigidbody _rb;
|
||||
private Vector3 _playerVelocity;
|
||||
private CharacterController _controller;
|
||||
private ContinuousMoveProvider _moveProvider;
|
||||
private float gravityValue = Physics.gravity.y;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rb = GetComponent<Rigidbody>();
|
||||
_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 (_rb == null)
|
||||
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)
|
||||
{
|
||||
Debug.LogWarning("[PlayerController] Rigidbody가 없어 점프할 수 없습니다.", this);
|
||||
return;
|
||||
// 완전히 0으로 두면 바닥 감지가 불안정할 수 있으므로 약간의 음수 값을 유지
|
||||
_playerVelocity.y = -2f;
|
||||
}
|
||||
|
||||
// 바닥에 닿아 있을 때만 점프 (공중 연속 점프 방지)
|
||||
if (!IsGrounded()) return;
|
||||
|
||||
// 수직 속도를 초기화한 뒤 위로 임펄스를 줘서 항상 같은 높이로 점프
|
||||
Vector3 v = _rb.linearVelocity;
|
||||
v.y = 0f;
|
||||
_rb.linearVelocity = v;
|
||||
_rb.AddForce(Vector3.up * _jumpForce, ForceMode.Impulse);
|
||||
}
|
||||
|
||||
// 발밑으로 짧은 레이를 쏴서 바닥 접촉 여부 확인
|
||||
private bool IsGrounded()
|
||||
{
|
||||
Vector3 origin = transform.position + Vector3.up * 0.05f;
|
||||
return Physics.Raycast(origin, Vector3.down, _groundCheckDistance + 0.05f, _groundLayer,
|
||||
QueryTriggerInteraction.Ignore);
|
||||
_playerVelocity.y += gravityValue * Time.deltaTime;
|
||||
_controller.Move(_playerVelocity * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user