[Add] New Player Input

This commit is contained in:
2026-04-08 21:07:56 +07:00
parent efce7c458d
commit 914d7a1778
3 changed files with 67 additions and 6 deletions
@@ -19,7 +19,7 @@ MonoBehaviour:
- id: EntranceCrypt
prefab: {fileID: 155468, guid: ea0c7071e67bf1c43940a8ab3ea121f8, type: 3}
weight: 10
spawnChancePercent: 15
spawnChancePercent: 5
placementMode: 1
footprint: {x: 2, y: 2}
clearance: 4
@@ -444,7 +444,12 @@ MonoBehaviour:
_addedNetworkObject: {fileID: 6486868354670318784}
_networkObjectCache: {fileID: 6486868354670318784}
_moveSpeed: 30
_sprintMultiplier: 2
_slowMultiplier: 0.5
_characterController: {fileID: 9116505237391369033}
_moveAction: {fileID: -1680190386980627800, guid: 2bcd2660ca9b64942af0de543d8d7100, type: 3}
_sprintAction: {fileID: -7471211009204490832, guid: 2bcd2660ca9b64942af0de543d8d7100, type: 3}
_slowAction: {fileID: 7040659881675835329, guid: 2bcd2660ca9b64942af0de543d8d7100, type: 3}
--- !u!114 &5771682633975047943
MonoBehaviour:
m_ObjectHideFlags: 0
+61 -5
View File
@@ -1,12 +1,18 @@
using FishNet.Object;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Players
{
public sealed class PlayerMoving : NetworkBehaviour
{
[SerializeField] private float _moveSpeed = 5f;
[SerializeField] private float _sprintMultiplier = 2f;
[SerializeField] private float _slowMultiplier = 0.5f;
[SerializeField] private CharacterController _characterController;
[SerializeField] private InputActionReference _moveAction;
[SerializeField] private InputActionReference _sprintAction;
[SerializeField] private InputActionReference _slowAction;
private Transform _cameraTransform;
@@ -16,22 +22,72 @@ namespace Players
if (playerCamera != null)
_cameraTransform = playerCamera.transform;
}
private void OnEnable()
{
EnableAction(_moveAction);
EnableAction(_sprintAction);
EnableAction(_slowAction);
}
private void OnDisable()
{
DisableAction(_moveAction);
DisableAction(_sprintAction);
DisableAction(_slowAction);
}
private void Update()
{
if (!IsOwner)
return;
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector2 moveInput = ReadMoveInput();
float speedMultiplier = ReadSpeedMultiplier();
Transform directionSource = _cameraTransform != null ? _cameraTransform : transform;
Vector3 forward = Vector3.ProjectOnPlane(directionSource.forward, Vector3.up).normalized;
Vector3 right = Vector3.ProjectOnPlane(directionSource.right, Vector3.up).normalized;
Vector3 moveDirection = (right * horizontal + forward * vertical).normalized;
Vector3 offset = moveDirection * (_moveSpeed * Time.deltaTime);
Vector3 moveDirection = (right * moveInput.x + forward * moveInput.y).normalized;
Vector3 offset = moveDirection * (_moveSpeed * speedMultiplier * Time.deltaTime);
_characterController.Move(offset);
}
private Vector2 ReadMoveInput()
{
InputAction action = _moveAction != null ? _moveAction.action : null;
return action != null ? action.ReadValue<Vector2>() : Vector2.zero;
}
private float ReadSpeedMultiplier()
{
bool isSprinting = IsActionPressed(_sprintAction);
bool isSlowing = IsActionPressed(_slowAction);
if (isSprinting == isSlowing)
return 1f;
return isSprinting ? _sprintMultiplier : _slowMultiplier;
}
private static bool IsActionPressed(InputActionReference actionReference)
{
InputAction action = actionReference != null ? actionReference.action : null;
return action != null && action.IsPressed();
}
private static void EnableAction(InputActionReference actionReference)
{
InputAction action = actionReference != null ? actionReference.action : null;
if (action != null && !action.enabled)
action.Enable();
}
private static void DisableAction(InputActionReference actionReference)
{
InputAction action = actionReference != null ? actionReference.action : null;
if (action != null && action.enabled)
action.Disable();
}
}
}