test
This commit is contained in:
79
Assets/charecter-controller/scripts/PlayerInput.cs
Normal file
79
Assets/charecter-controller/scripts/PlayerInput.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public class PlayerInput : MonoBehaviour
|
||||
{
|
||||
public static PlayerInput Instance { get; private set; }
|
||||
|
||||
// Continuous axes
|
||||
public float MouseX { get; private set; }
|
||||
public float MouseY { get; private set; }
|
||||
public float Horizontal { get; private set; }
|
||||
public float Vertical { get; private set; }
|
||||
|
||||
// Continuous states
|
||||
public bool Crouch { get; private set; }
|
||||
public bool Run { get; private set; }
|
||||
|
||||
// Events for discrete actions
|
||||
public event Action OnToggleInventory;
|
||||
public event Action OnDropPressed;
|
||||
public event Action OnPickupPressed;
|
||||
public event Action OnJumpPressed;
|
||||
public event Action<int> OnHotbarSelect;
|
||||
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Always allow toggle inventory (Tab/Escape) even when input is disabled
|
||||
if (Input.GetKeyDown(KeyCode.Tab) || Input.GetKeyDown(KeyCode.Escape))
|
||||
OnToggleInventory?.Invoke();
|
||||
|
||||
if (!Enabled) return;
|
||||
|
||||
// Axes
|
||||
MouseX = Input.GetAxis("Mouse X");
|
||||
MouseY = Input.GetAxis("Mouse Y");
|
||||
Horizontal = Input.GetAxisRaw("Horizontal");
|
||||
Vertical = Input.GetAxisRaw("Vertical");
|
||||
|
||||
// Continuous keys
|
||||
Crouch = Input.GetKey(KeyCode.C);
|
||||
Run = Input.GetKey(KeyCode.LeftShift);
|
||||
|
||||
// Discrete actions
|
||||
if (Input.GetButtonDown("Jump"))
|
||||
OnJumpPressed?.Invoke();
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Tab))
|
||||
OnToggleInventory?.Invoke();
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Q))
|
||||
OnDropPressed?.Invoke();
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.E))
|
||||
OnPickupPressed?.Invoke();
|
||||
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Alpha1 + i))
|
||||
{
|
||||
OnHotbarSelect?.Invoke(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/charecter-controller/scripts/PlayerInput.cs.meta
Normal file
2
Assets/charecter-controller/scripts/PlayerInput.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cb5fd82e043d6694e90df66d2b3f267a
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user