From 5772da3965c671c56a15f20c22ec2d8cc7984759 Mon Sep 17 00:00:00 2001 From: BuyMyMojo Date: Tue, 28 Feb 2023 03:19:49 +1100 Subject: [PATCH] Add interaction input handling --- .erp | 2 +- Assets/PlayerInputActions.cs | 41 ++++++++++++++++++++++++++ Assets/PlayerInputActions.inputactions | 31 +++++++++++++++++++ Assets/Scripts/GameInput.cs | 13 ++++++++ Assets/Scripts/Player.cs | 31 ++++++++++++++++++- 5 files changed, 116 insertions(+), 2 deletions(-) diff --git a/.erp b/.erp index b1c5956..7386e91 100644 --- a/.erp +++ b/.erp @@ -5,7 +5,7 @@ false false true - 1677515252 + 1677518740 522683637854076046 false \ No newline at end of file diff --git a/Assets/PlayerInputActions.cs b/Assets/PlayerInputActions.cs index 4297a7b..85f3eef 100644 --- a/Assets/PlayerInputActions.cs +++ b/Assets/PlayerInputActions.cs @@ -35,6 +35,15 @@ public partial class @PlayerInputActions: IInputActionCollection2, IDisposable ""processors"": """", ""interactions"": """", ""initialStateCheck"": true + }, + { + ""name"": ""Interact"", + ""type"": ""Button"", + ""id"": ""6d76184b-c071-44cb-b813-020b6a80a5eb"", + ""expectedControlType"": ""Button"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": false } ], ""bindings"": [ @@ -158,6 +167,28 @@ public partial class @PlayerInputActions: IInputActionCollection2, IDisposable ""action"": ""Move"", ""isComposite"": false, ""isPartOfComposite"": false + }, + { + ""name"": """", + ""id"": ""748d47d0-26ed-4681-b4e6-444c65489c76"", + ""path"": ""/e"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Interact"", + ""isComposite"": false, + ""isPartOfComposite"": false + }, + { + ""name"": """", + ""id"": ""f3791e57-b19a-4a65-ba91-e9f4484e7581"", + ""path"": ""/buttonWest"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Interact"", + ""isComposite"": false, + ""isPartOfComposite"": false } ] } @@ -167,6 +198,7 @@ public partial class @PlayerInputActions: IInputActionCollection2, IDisposable // Player m_Player = asset.FindActionMap("Player", throwIfNotFound: true); m_Player_Move = m_Player.FindAction("Move", throwIfNotFound: true); + m_Player_Interact = m_Player.FindAction("Interact", throwIfNotFound: true); } public void Dispose() @@ -229,11 +261,13 @@ public partial class @PlayerInputActions: IInputActionCollection2, IDisposable private readonly InputActionMap m_Player; private List m_PlayerActionsCallbackInterfaces = new List(); private readonly InputAction m_Player_Move; + private readonly InputAction m_Player_Interact; public struct PlayerActions { private @PlayerInputActions m_Wrapper; public PlayerActions(@PlayerInputActions wrapper) { m_Wrapper = wrapper; } public InputAction @Move => m_Wrapper.m_Player_Move; + public InputAction @Interact => m_Wrapper.m_Player_Interact; public InputActionMap Get() { return m_Wrapper.m_Player; } public void Enable() { Get().Enable(); } public void Disable() { Get().Disable(); } @@ -246,6 +280,9 @@ public partial class @PlayerInputActions: IInputActionCollection2, IDisposable @Move.started += instance.OnMove; @Move.performed += instance.OnMove; @Move.canceled += instance.OnMove; + @Interact.started += instance.OnInteract; + @Interact.performed += instance.OnInteract; + @Interact.canceled += instance.OnInteract; } private void UnregisterCallbacks(IPlayerActions instance) @@ -253,6 +290,9 @@ public partial class @PlayerInputActions: IInputActionCollection2, IDisposable @Move.started -= instance.OnMove; @Move.performed -= instance.OnMove; @Move.canceled -= instance.OnMove; + @Interact.started -= instance.OnInteract; + @Interact.performed -= instance.OnInteract; + @Interact.canceled -= instance.OnInteract; } public void RemoveCallbacks(IPlayerActions instance) @@ -273,5 +313,6 @@ public partial class @PlayerInputActions: IInputActionCollection2, IDisposable public interface IPlayerActions { void OnMove(InputAction.CallbackContext context); + void OnInteract(InputAction.CallbackContext context); } } diff --git a/Assets/PlayerInputActions.inputactions b/Assets/PlayerInputActions.inputactions index f730c98..b0a0ecf 100644 --- a/Assets/PlayerInputActions.inputactions +++ b/Assets/PlayerInputActions.inputactions @@ -13,6 +13,15 @@ "processors": "", "interactions": "", "initialStateCheck": true + }, + { + "name": "Interact", + "type": "Button", + "id": "6d76184b-c071-44cb-b813-020b6a80a5eb", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false } ], "bindings": [ @@ -136,6 +145,28 @@ "action": "Move", "isComposite": false, "isPartOfComposite": false + }, + { + "name": "", + "id": "748d47d0-26ed-4681-b4e6-444c65489c76", + "path": "/e", + "interactions": "", + "processors": "", + "groups": "", + "action": "Interact", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "f3791e57-b19a-4a65-ba91-e9f4484e7581", + "path": "/buttonWest", + "interactions": "", + "processors": "", + "groups": "", + "action": "Interact", + "isComposite": false, + "isPartOfComposite": false } ] } diff --git a/Assets/Scripts/GameInput.cs b/Assets/Scripts/GameInput.cs index 33869ca..6d8a645 100644 --- a/Assets/Scripts/GameInput.cs +++ b/Assets/Scripts/GameInput.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -5,13 +6,25 @@ using UnityEngine; public class GameInput : MonoBehaviour { + public event EventHandler OnInteractAction; + + private PlayerInputActions playerInputActions; + private void Awake() { playerInputActions = new PlayerInputActions(); playerInputActions.Player.Enable(); + + playerInputActions.Player.Interact.performed += Interact_performed; } + + private void Interact_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj) + { + OnInteractAction?.Invoke(this, EventArgs.Empty); + } + public Vector2 GetMovementVectorNormalized() { Vector2 inputVector = playerInputActions.Player.Move.ReadValue(); diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index 09e8542..6f70b52 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -12,6 +12,35 @@ public class Player : MonoBehaviour private bool isWalking; private Vector3 lastInteractDir; + private void Start() + { + gameInput.OnInteractAction += GameInput_OnInteractAction; + } + + private void GameInput_OnInteractAction(object sender, System.EventArgs e) + { + Vector2 inputVector = gameInput.GetMovementVectorNormalized(); + + Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y); + + // Kepp a constantly updating record of the last move direction. + // This means you can interact without holding down a direction. + if (moveDir != Vector3.zero) + { + lastInteractDir = moveDir; + } + + float interactDistance = 2f; + if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask)) + { + if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter)) + { + // Has Clear Counter + clearCounter.Interact(); + } + } + } + private void Update() { HandleMovement(); @@ -45,7 +74,7 @@ public class Player : MonoBehaviour if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter)) { // Has Clear Counter - clearCounter.Interact(); + //clearCounter.Interact(); } } }