It's because I tested the distance that I was figuring out why I kept getting a hit each time.
Look Direction
You want to set this where you set that lastMoveX in the animator.
public void OnMove(InputAction.CallbackContext ctx)
{
...
if (movementInput.x == 1 || movementInput.x == -1 || movementInput.y == 1 || movementInput.y == -1)
...
lookDirection = new Vector2(movementInput.x, movementInput.y);
}
}
Button Firing too many times in Input System
// Example Code
public void OnMyAction(InputAction.CallbackContext context)
{
if (context.started) {
Debug.Log("Action was started");
}
else if (context.performed) {
Debug.Log("Action was performed");
}
else if (context.canceled){
Debug.Log("Action was cancelled");
}
}
For our project:
public void OnInteract(InputAction.CallbackContext ctx)
{
if (ctx.started) {
Debug.Log("DiaManager Should Open");
}
}
Script adapted from Ruby's Adventure
RaycastHit2D hit = Physics2D.Raycast(rb.position + Vector2.up, lookDirection, 1.5f, LayerMask.GetMask("Interactibles"));
if (hit.collider != null)
{
DiaTrigger character = hit.collider.GetComponent<DiaTrigger>();
if (character != null)
{
character.Begin();
}
}
Disabling Movement for UI
To switch to UI, you can change the Action Map. You need to set the default action map in the previous section first before switching.
This Interact function will trigger the DiaManager's "Show" function (This is the UI controller for the dialogue box). That is when we want to stop the player from moving.
using UnityEngine.InputSystem;
...
private PlayerInput playerInput;
//Initial State of the Game
private void Start()
{
//I've put the playerInput component on the Player GameObject, hence the tag
playerInput = GameObject.FindWithTag("Player").GetComponent<PlayerInput>();
}
public void Show(...)
{
Debug.Log("DiaManager Should Open");
playerInput.SwitchCurrentActionMap("UI");
}