From 914d7a17781f361d2242e2c6d3e94ba1a1778d80 Mon Sep 17 00:00:00 2001 From: Konstantin Dyachenko Date: Wed, 8 Apr 2026 21:07:56 +0700 Subject: [PATCH] [Add] New Player Input --- .../Data/WorldPrefabCollection.asset | 2 +- .../VoxelWorld/Prefabs/TestPlayer.prefab | 5 ++ Assets/Scripts/Players/PlayerMoving.cs | 66 +++++++++++++++++-- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/Assets/Features/VoxelWorld/Data/WorldPrefabCollection.asset b/Assets/Features/VoxelWorld/Data/WorldPrefabCollection.asset index b1c7a448..cc0ff875 100644 --- a/Assets/Features/VoxelWorld/Data/WorldPrefabCollection.asset +++ b/Assets/Features/VoxelWorld/Data/WorldPrefabCollection.asset @@ -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 diff --git a/Assets/Features/VoxelWorld/Prefabs/TestPlayer.prefab b/Assets/Features/VoxelWorld/Prefabs/TestPlayer.prefab index 0c843d96..24db32a0 100644 --- a/Assets/Features/VoxelWorld/Prefabs/TestPlayer.prefab +++ b/Assets/Features/VoxelWorld/Prefabs/TestPlayer.prefab @@ -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 diff --git a/Assets/Scripts/Players/PlayerMoving.cs b/Assets/Scripts/Players/PlayerMoving.cs index 5e59a7d2..df6149aa 100644 --- a/Assets/Scripts/Players/PlayerMoving.cs +++ b/Assets/Scripts/Players/PlayerMoving.cs @@ -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.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(); + } } }