Add interaction input handling
This commit is contained in:
parent
72a0daf486
commit
5772da3965
5 changed files with 116 additions and 2 deletions
|
@ -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>();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue