This commit is contained in:
unknown
2026-04-15 20:04:28 +05:00
parent 0837589fa2
commit 057f5d3d1e
6 changed files with 198 additions and 45 deletions

View File

@@ -31,6 +31,7 @@ public class charecter : MonoBehaviour
float _headYaw;
float _pitch;
Quaternion _headBindPose;
bool _jumpRequested = false;
// Animator parameter hashes — no hardcoded strings at runtime
static readonly int HashSpeed = Animator.StringToHash("Speed");
@@ -49,22 +50,28 @@ public class charecter : MonoBehaviour
_bodyYaw = transform.eulerAngles.y;
_headYaw = _bodyYaw;
_headBindPose = head.localRotation;
// Ensure PlayerInput exists and subscribe to events
if (PlayerInput.Instance == null)
{
var go = new GameObject("PlayerInput");
go.AddComponent<PlayerInput>();
}
PlayerInput.Instance.OnPickupPressed += () => TryPickupNearbyItem();
PlayerInput.Instance.OnJumpPressed += () => { _jumpRequested = true; };
}
void Update()
{
HandleLook();
HandleMovement();
HandlePickup();
}
// ── Item Pickup ───────────────────────────────────────────────────────────
void HandlePickup()
{
if (Input.GetKeyDown(KeyCode.E))
{
TryPickupNearbyItem();
}
// Input moved to PlayerInput events
}
void TryPickupNearbyItem()
@@ -115,19 +122,24 @@ public class charecter : MonoBehaviour
// ── Mouse look ────────────────────────────────────────────────────────────
void HandleLook()
{
_headYaw += Input.GetAxis("Mouse X") * mouseSensitivity;
_pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
var input = PlayerInput.Instance;
if (input != null)
{
_headYaw += input.MouseX * mouseSensitivity;
_pitch -= input.MouseY * mouseSensitivity;
}
_pitch = Mathf.Clamp(_pitch, -maxPitch, maxPitch);
}
// ── Movement ──────────────────────────────────────────────────────────────
void HandleMovement()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
var input = PlayerInput.Instance;
float h = input != null ? input.Horizontal : Input.GetAxisRaw("Horizontal");
float v = input != null ? input.Vertical : Input.GetAxisRaw("Vertical");
bool isCrouching = Input.GetKey(KeyCode.C);
bool isRunning = Input.GetKey(KeyCode.LeftShift) && !isCrouching;
bool isCrouching = input != null ? input.Crouch : Input.GetKey(KeyCode.C);
bool isRunning = input != null ? input.Run && !isCrouching : Input.GetKey(KeyCode.LeftShift) && !isCrouching;
bool isMoving = (h * h + v * v) > 0.01f;
float speed = isCrouching ? crouchSpeed : isRunning ? runSpeed : walkSpeed;
@@ -147,11 +159,12 @@ public class charecter : MonoBehaviour
{
if (_verticalVelocity < 0f) _verticalVelocity = -2f;
if (Input.GetButtonDown("Jump") && !isCrouching)
if (_jumpRequested && !isCrouching)
{
_verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * gravity);
_anim?.SetTrigger(HashJump);
}
_jumpRequested = false;
}
_verticalVelocity += gravity * Time.deltaTime;