[Add] New Player Input
This commit is contained in:
@@ -19,7 +19,7 @@ MonoBehaviour:
|
|||||||
- id: EntranceCrypt
|
- id: EntranceCrypt
|
||||||
prefab: {fileID: 155468, guid: ea0c7071e67bf1c43940a8ab3ea121f8, type: 3}
|
prefab: {fileID: 155468, guid: ea0c7071e67bf1c43940a8ab3ea121f8, type: 3}
|
||||||
weight: 10
|
weight: 10
|
||||||
spawnChancePercent: 15
|
spawnChancePercent: 5
|
||||||
placementMode: 1
|
placementMode: 1
|
||||||
footprint: {x: 2, y: 2}
|
footprint: {x: 2, y: 2}
|
||||||
clearance: 4
|
clearance: 4
|
||||||
|
|||||||
@@ -444,7 +444,12 @@ MonoBehaviour:
|
|||||||
_addedNetworkObject: {fileID: 6486868354670318784}
|
_addedNetworkObject: {fileID: 6486868354670318784}
|
||||||
_networkObjectCache: {fileID: 6486868354670318784}
|
_networkObjectCache: {fileID: 6486868354670318784}
|
||||||
_moveSpeed: 30
|
_moveSpeed: 30
|
||||||
|
_sprintMultiplier: 2
|
||||||
|
_slowMultiplier: 0.5
|
||||||
_characterController: {fileID: 9116505237391369033}
|
_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
|
--- !u!114 &5771682633975047943
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
using FishNet.Object;
|
using FishNet.Object;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.InputSystem;
|
||||||
|
|
||||||
namespace Players
|
namespace Players
|
||||||
{
|
{
|
||||||
public sealed class PlayerMoving : NetworkBehaviour
|
public sealed class PlayerMoving : NetworkBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] private float _moveSpeed = 5f;
|
[SerializeField] private float _moveSpeed = 5f;
|
||||||
|
[SerializeField] private float _sprintMultiplier = 2f;
|
||||||
|
[SerializeField] private float _slowMultiplier = 0.5f;
|
||||||
[SerializeField] private CharacterController _characterController;
|
[SerializeField] private CharacterController _characterController;
|
||||||
|
[SerializeField] private InputActionReference _moveAction;
|
||||||
|
[SerializeField] private InputActionReference _sprintAction;
|
||||||
|
[SerializeField] private InputActionReference _slowAction;
|
||||||
|
|
||||||
private Transform _cameraTransform;
|
private Transform _cameraTransform;
|
||||||
|
|
||||||
@@ -16,22 +22,72 @@ namespace Players
|
|||||||
if (playerCamera != null)
|
if (playerCamera != null)
|
||||||
_cameraTransform = playerCamera.transform;
|
_cameraTransform = playerCamera.transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnEnable()
|
||||||
|
{
|
||||||
|
EnableAction(_moveAction);
|
||||||
|
EnableAction(_sprintAction);
|
||||||
|
EnableAction(_slowAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDisable()
|
||||||
|
{
|
||||||
|
DisableAction(_moveAction);
|
||||||
|
DisableAction(_sprintAction);
|
||||||
|
DisableAction(_slowAction);
|
||||||
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
if (!IsOwner)
|
if (!IsOwner)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
float horizontal = Input.GetAxisRaw("Horizontal");
|
Vector2 moveInput = ReadMoveInput();
|
||||||
float vertical = Input.GetAxisRaw("Vertical");
|
float speedMultiplier = ReadSpeedMultiplier();
|
||||||
|
|
||||||
Transform directionSource = _cameraTransform != null ? _cameraTransform : transform;
|
Transform directionSource = _cameraTransform != null ? _cameraTransform : transform;
|
||||||
Vector3 forward = Vector3.ProjectOnPlane(directionSource.forward, Vector3.up).normalized;
|
Vector3 forward = Vector3.ProjectOnPlane(directionSource.forward, Vector3.up).normalized;
|
||||||
Vector3 right = Vector3.ProjectOnPlane(directionSource.right, Vector3.up).normalized;
|
Vector3 right = Vector3.ProjectOnPlane(directionSource.right, Vector3.up).normalized;
|
||||||
Vector3 moveDirection = (right * horizontal + forward * vertical).normalized;
|
Vector3 moveDirection = (right * moveInput.x + forward * moveInput.y).normalized;
|
||||||
Vector3 offset = moveDirection * (_moveSpeed * Time.deltaTime);
|
Vector3 offset = moveDirection * (_moveSpeed * speedMultiplier * Time.deltaTime);
|
||||||
|
|
||||||
_characterController.Move(offset);
|
_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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user