Add interaction input handling

This commit is contained in:
BuyMyMojo 2023-02-28 03:19:49 +11:00
parent 72a0daf486
commit 5772da3965
5 changed files with 116 additions and 2 deletions

2
.erp
View file

@ -5,7 +5,7 @@
<resetOnSceneChange>false</resetOnSceneChange>
<debugMode>false</debugMode>
<EditorClosed>true</EditorClosed>
<LastTimestamp>1677515252</LastTimestamp>
<LastTimestamp>1677518740</LastTimestamp>
<LastSessionID>522683637854076046</LastSessionID>
<Errored>false</Errored>
</ERPSettings>

View file

@ -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"": ""<Keyboard>/e"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Interact"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""f3791e57-b19a-4a65-ba91-e9f4484e7581"",
""path"": ""<Gamepad>/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<IPlayerActions> m_PlayerActionsCallbackInterfaces = new List<IPlayerActions>();
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);
}
}

View file

@ -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": "<Keyboard>/e",
"interactions": "",
"processors": "",
"groups": "",
"action": "Interact",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "f3791e57-b19a-4a65-ba91-e9f4484e7581",
"path": "<Gamepad>/buttonWest",
"interactions": "",
"processors": "",
"groups": "",
"action": "Interact",
"isComposite": false,
"isPartOfComposite": false
}
]
}

View file

@ -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<Vector2>();

View file

@ -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();
}
}
}