Commit f3e0b933 by alsunj

Merge branch 'feat/new-input-system' into 'main'

Feat/new input system

See merge request alsunj/loputoo!3
parents f535a480 665dc919
......@@ -15,10 +15,10 @@ public class PlayerAnimator : NetworkBehaviour
private void Update()
{
if (!IsOwner)
{
return;
}
// if (!IsOwner)
// {
// return;
// }
_animator.SetBool(IS_WALKING, _playerController.IsWalking());
}
......
using System;
using Unity.Netcode;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
public class PlayerController : NetworkBehaviour
{
[SerializeField] private PlayerInteractionSettings _playerInteractionSettings;
[SerializeField] private float _speed = 2f;
[SerializeField] private PlayerInteractionSettings playerInteractionSettings;
[SerializeField] private float speed = 2f;
[SerializeField] private InputReader _inputReader;
private Rigidbody _rb;
[SerializeField] private Camera _camera;
private Camera _camera;
public Vector3 offset = new Vector3(0, 7.4f, -6.4f);
public Vector3 eulerAngles = new Vector3(40.45f, 0, 0);
private bool _isWalking;
//camera public Vector3 eulerAngles = new Vector3(40.45f, 0, 0);
private Vector2 _movementInput;
public float fov = 60;
void Start()
public bool enableSprint = true;
public bool unlimitedSprint = false;
public float sprintSpeed = 7f;
public float sprintDuration = 5f;
public float sprintCooldown = .5f;
public float sprintFOV = 80f;
public float sprintFOVStepTime = 10f;
public float zoomStepTime = 5f;
public bool playerCanMove = true;
public float walkSpeed = 5f;
public bool _isWalking = false;
public float maxVelocityChange = 10f;
// Internal Variables
private bool _isSprinting = false;
private float _sprintRemaining;
private bool _isSprintCooldown = false;
private float _sprintCooldownReset;
// Internal Variables
private Vector3 _jointOriginalPos;
private float _timer = 0;
private float _walkingSoundTimer = 0;
private bool _isWalkingSoundCooldown = false;
private float _sprintingSoundTimer = 0;
private bool _isSprintingSoundCooldown = false;
private void OnEnable()
{
if (_inputReader != null)
{
_inputReader.MoveEvent += OnMove;
_inputReader.InteractEvent += OnInteract;
_inputReader.SprintEvent += OnSprint;
}
}
private void OnDisable()
{
if (_inputReader != null)
{
_inputReader.MoveEvent -= OnMove;
_inputReader.InteractEvent -= OnInteract;
_inputReader.SprintEvent -= OnSprint;
}
}
private void Start()
{
_camera = GetComponentInChildren<Camera>();
_inputReader.InitializeInput();
_rb = GetComponent<Rigidbody>();
_rb.isKinematic = false; // Ensure isKinematic is false
}
void Update()
private void OnSprint(bool state)
{
// if (!IsOwner)
// {
// return;
// }
_isSprinting = state;
}
if (Input.GetKeyDown(KeyCode.E))
private void OnMove(Vector2 movement)
{
CheckForInteractableCollision();
_movementInput = movement;
}
private void Update()
{
if (enableSprint)
{
if (_isSprinting && !_isSprintCooldown)
{
if (_isSprintingSoundCooldown)
{
// if flash is on cooldown, increase the timer
_sprintingSoundTimer += Time.deltaTime;
// if the timer is greater than the cooldown, refresh the cooldown boolean and reset the timer
if (_sprintingSoundTimer >= 0.3f)
{
_isSprintingSoundCooldown = false;
_sprintingSoundTimer = 0f;
}
}
if (!_isSprintingSoundCooldown)
{
_isSprintingSoundCooldown = true;
}
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, sprintFOV,
sprintFOVStepTime * Time.deltaTime);
// Drain sprint remaining while sprinting
if (!unlimitedSprint)
{
_sprintRemaining -= 1 * Time.deltaTime;
if (_sprintRemaining <= 0)
{
_isSprinting = false;
_isSprintCooldown = true;
}
}
}
else
{
// Regain sprint while not sprinting
_sprintRemaining = Mathf.Clamp(_sprintRemaining += 1 * Time.deltaTime, 0, sprintDuration);
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, fov, zoomStepTime * Time.deltaTime);
}
UpdateMovement();
// Handles sprint cooldown
// When sprint remaining == 0 stops sprint ability until hitting cooldown
if (_isSprintCooldown)
{
sprintCooldown -= 1 * Time.deltaTime;
if (sprintCooldown <= 0)
{
_isSprintCooldown = false;
}
}
else
{
sprintCooldown = _sprintCooldownReset;
}
}
}
private void UpdateMovement()
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical).normalized * _speed * Time.fixedDeltaTime;
if (movement != Vector3.zero)
#region Movement
if (playerCanMove)
{
// Use the input from _movementInput to determine movement
Vector3 targetVelocity = new Vector3(_movementInput.x, 0, _movementInput.y);
// Checks if player is walking and is grounded
if (targetVelocity.x != 0 || targetVelocity.z != 0)
{
_rb.MovePosition(transform.position + movement);
Debug.Log(movement);
Quaternion targetRotation = Quaternion.LookRotation(movement);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, _speed);
_camera.transform.rotation = Quaternion.Slerp(_camera.transform.rotation,
targetRotation * Quaternion.Euler(eulerAngles), _speed);
_isWalking = true;
if (_isWalkingSoundCooldown)
{
// if flash is on cooldown, increase the timer
_walkingSoundTimer += Time.fixedDeltaTime;
// if the timer is greater than the cooldown, refresh the cooldown boolean and reset the timer
if (_walkingSoundTimer >= 0.5f)
{
_isWalkingSoundCooldown = false;
_walkingSoundTimer = 0f;
}
}
if (!_isWalkingSoundCooldown)
{
_isWalkingSoundCooldown = true;
}
}
else
{
_isWalking = false;
}
// All movement calculations while sprint is active
if (enableSprint && _isSprinting && _sprintRemaining > 0f && !_isSprintCooldown)
{
targetVelocity = transform.TransformDirection(targetVelocity) * sprintSpeed;
Vector3 velocity = _rb.linearVelocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
if (velocityChange.x != 0 || velocityChange.z != 0)
{
_isSprinting = true;
}
_rb.AddForce(velocityChange, ForceMode.VelocityChange);
}
// All movement calculations while walking
else
{
_isSprinting = false;
targetVelocity = transform.TransformDirection(targetVelocity) * walkSpeed;
Vector3 velocity = _rb.linearVelocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
_rb.AddForce(velocityChange, ForceMode.VelocityChange);
}
if (targetVelocity != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(targetVelocity);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.fixedDeltaTime);
}
}
UpdateCamera();
UpdateMovementBooleans();
#endregion
}
private void OnInteract()
{
Debug.Log("Interact");
CheckForInteractableCollision();
}
private void UpdateCamera()
{
_camera.transform.position = gameObject.transform.position + offset;
_camera.transform.localPosition = offset;
_camera.transform.localRotation = Quaternion.Euler(40.45f, 0, 0);
}
private void CheckForInteractableCollision()
{
Collider[] hitColliders = Physics.OverlapSphere(transform.position,
_playerInteractionSettings.interactableRadius,
_playerInteractionSettings.interactableLayer);
playerInteractionSettings.interactableRadius,
playerInteractionSettings.interactableLayer);
foreach (var hitCollider in hitColliders)
{
switch (hitCollider.GetComponent<IInteractable>())
......
fileFormatVersion: 2
guid: 0343730f57b4d2a48895245cfc7f63b1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System;
using UnityEngine;
public interface IInputHandler
{
event Action<Vector2> MoveEvent;
event Action<Vector2> LookEvent;
event Action InteractEvent;
event Action JumpEvent;
event Action<bool> SprintEvent;
event Action<bool> CrouchEvent;
event Action AttackEvent;
void SimulateMove(Vector2 movement);
void SimulateInteract();
void SimulateSprint(bool isSprinting);
}
\ No newline at end of file
fileFormatVersion: 2
guid: 24d81946a3a22194e90d36523060f029
\ No newline at end of file
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 38046858527cd1b4fa2edbe312d75842, type: 3}
m_Name: InputReader
m_EditorClassIdentifier:
fileFormatVersion: 2
guid: fae963ae99ce6d14d8bbcd54becb58d7
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:
using System;
using UnityEngine;
using UnityEngine.InputSystem;
[CreateAssetMenu(fileName = "InputReader", menuName = "Scriptable Objects/InputReader")]
public class InputReader : ScriptableObject, InputSystem_Actions.IPlayerActions, IInputHandler
{
private InputSystem_Actions inputActions;
public event Action<Vector2> MoveEvent;
public event Action<Vector2> LookEvent;
public event Action InteractEvent;
public event Action JumpEvent;
public event Action<bool> SprintEvent;
public event Action<bool> CrouchEvent;
public event Action AttackEvent;
public void InitializeInput()
{
if (inputActions == null)
{
inputActions = new InputSystem_Actions();
inputActions.Player.SetCallbacks(this);
}
inputActions.Enable();
}
private void OnDisable()
{
if (inputActions != null)
{
inputActions.Disable();
inputActions.Player.RemoveCallbacks(this);
inputActions.Dispose();
}
}
public void OnMove(InputAction.CallbackContext context)
{
if (context.performed)
{
MoveEvent?.Invoke(context.ReadValue<Vector2>());
}
else
{
MoveEvent?.Invoke(Vector2.zero);
}
}
public void OnLook(InputAction.CallbackContext context)
{
if (context.performed)
{
LookEvent?.Invoke(context.ReadValue<Vector2>());
}
else
{
LookEvent?.Invoke(new Vector2(0, 0));
}
}
public void OnAttack(InputAction.CallbackContext context)
{
if (context.performed)
{
AttackEvent?.Invoke();
}
}
public void OnInteract(InputAction.CallbackContext context)
{
if (context.performed)
{
InteractEvent?.Invoke();
}
}
public void OnCrouch(InputAction.CallbackContext context)
{
if (context.performed)
{
CrouchEvent?.Invoke(true);
}
else
{
CrouchEvent?.Invoke(false);
}
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed)
{
JumpEvent?.Invoke();
}
}
public void OnSprint(InputAction.CallbackContext context)
{
if (context.performed)
{
SprintEvent?.Invoke(true);
}
else
{
SprintEvent?.Invoke(false);
}
}
public void SimulateMove(Vector2 movement)
{
throw new NotImplementedException();
}
public void SimulateInteract()
{
throw new NotImplementedException();
}
public void SimulateSprint(bool isSprinting)
{
throw new NotImplementedException();
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 38046858527cd1b4fa2edbe312d75842
\ No newline at end of file
fileFormatVersion: 2
guid: 2e363bf247c9d6d459bcda6fa30f8347
\ No newline at end of file
......@@ -27,7 +27,7 @@
"name": "Attack",
"type": "Button",
"id": "6c2ab1b8-8984-453a-af3d-a3c78ae1679a",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
......@@ -36,7 +36,7 @@
"name": "Interact",
"type": "Button",
"id": "852140f2-7766-474d-8707-702459ba45f3",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "Hold",
"initialStateCheck": false
......@@ -45,7 +45,7 @@
"name": "Crouch",
"type": "Button",
"id": "27c5f898-bc57-4ee1-8800-db469aca5fe3",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
......@@ -54,25 +54,7 @@
"name": "Jump",
"type": "Button",
"id": "f1ba0d36-48eb-4cd5-b651-1c94a6531f70",
"expectedControlType": "Button",
"processors": "",
"interactions": "",
"initialStateCheck": false
},
{
"name": "Previous",
"type": "Button",
"id": "2776c80d-3c14-4091-8c56-d04ced07a2b0",
"expectedControlType": "Button",
"processors": "",
"interactions": "",
"initialStateCheck": false
},
{
"name": "Next",
"type": "Button",
"id": "b7230bb6-fc9b-4f52-8b25-f5e19cb2c2ba",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
......@@ -81,7 +63,7 @@
"name": "Sprint",
"type": "Button",
"id": "641cd816-40e6-41b4-8c3d-04687c349290",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
......@@ -321,28 +303,6 @@
},
{
"name": "",
"id": "cbac6039-9c09-46a1-b5f2-4e5124ccb5ed",
"path": "<Keyboard>/2",
"interactions": "",
"processors": "",
"groups": "Keyboard&Mouse",
"action": "Next",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "e15ca19d-e649-4852-97d5-7fe8ccc44e94",
"path": "<Gamepad>/dpad/right",
"interactions": "",
"processors": "",
"groups": "Gamepad",
"action": "Next",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "f2e9ba44-c423-42a7-ad56-f20975884794",
"path": "<Keyboard>/leftShift",
"interactions": "",
......@@ -409,28 +369,6 @@
},
{
"name": "",
"id": "1534dc16-a6aa-499d-9c3a-22b47347b52a",
"path": "<Keyboard>/1",
"interactions": "",
"processors": "",
"groups": "Keyboard&Mouse",
"action": "Previous",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "25060bbd-a3a6-476e-8fba-45ae484aad05",
"path": "<Gamepad>/dpad/left",
"interactions": "",
"processors": "",
"groups": "Gamepad",
"action": "Previous",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "1c04ea5f-b012-41d1-a6f7-02e963b52893",
"path": "<Keyboard>/e",
"interactions": "",
......
......@@ -8,7 +8,7 @@ ScriptedImporter:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3}
generateWrapperCode: 0
generateWrapperCode: 1
wrapperCodePath:
wrapperClassName:
wrapperCodeNamespace:
fileFormatVersion: 2
guid: 4bdbbb25aff08a54098c34d6f75b201b
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -11,6 +11,9 @@ EditorBuildSettings:
- enabled: 1
path: Assets/Scenes/Scene.unity
guid: fb476371a58224b439cbcc569f3d86f0
- enabled: 1
path: Assets/_Game/Scenes/SC.unity
guid: 4bdbbb25aff08a54098c34d6f75b201b
m_configObjects:
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 052faaac586de48259a63d0c4782560b, type: 3}
m_UseUCBPForAssetBundles: 0
......@@ -25,7 +25,7 @@ EditorSettings:
m_AsyncShaderCompilation: 1
m_PrefabModeAllowAutoSave: 1
m_EnterPlayModeOptionsEnabled: 1
m_EnterPlayModeOptions: 0
m_EnterPlayModeOptions: 1
m_GameObjectNamingDigits: 1
m_GameObjectNamingScheme: 0
m_AssetNamingUsesSpace: 1
......
......@@ -945,7 +945,7 @@ PlayerSettings:
qnxGraphicConfPath:
apiCompatibilityLevel: 6
captureStartupLogs: {}
activeInputHandler: 2
activeInputHandler: 1
windowsGamepadBackendHint: 0
cloudProjectId: 3cd49368-e13d-4741-80b2-b4559fc6fe1b
framebufferDepthMemorylessMode: 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment