Hey guys,
So, I’m writing a simple 2D player script with an input manager. I’m trying to have a “Started” action that fires an OnMoveStart event, and a “Performed” action that fires an OnMoveEnd event. I’m doing this so I can continuously call the OnMoveStart method via a boolean in Update so the player can hold down the forward key and stay moving. The problem seems to be that both started and performed actions are firing simultaneously, as noted by a Debug.Log. I thought ‘Started’ happened when the key was initially pressed, and ‘Performed’ happened when the key was released? Is this not the case? Is there something else I’m doing wrong? Thanks for any help. Here’s the Input Manager:
.
public class InputManager : MonoBehaviour
{
public static Controls inputActionAsset;
[SerializeField] public PlayerController Player;
private void OnEnable()
{
inputActionAsset = new Controls();
inputActionAsset.Player.Move.started += Player.OnMoveStart;
inputActionAsset.Player.Move.performed += Player.OnMoveEnd;
inputActionAsset.Enable();
}
private void Update()
{
if (Player.moving)
{
Player.rb.velocity = new Vector2(Player.moveSpeed * Player.multiplier, 0);
}
}
.
and the PlayerController script:
.
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
[SerializeField] public int moveSpeed;
public float multiplier;
public bool moving;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
//if the player starts the action, set the move bool to true
//if the player performs the action, set the move bool to false
//this move bool calls the move action in update, so if it's true, it'll keep firing
public void OnMoveStart(InputAction.CallbackContext context)
{
multiplier = InputManager.inputActionAsset.Player.Move.ReadValue<float>();
rb.velocity = new Vector2(moveSpeed * multiplier, 0);
moving = true;
Debug.Log(moving.ToString());
}
public void OnMoveEnd(InputAction.CallbackContext context)
{
moving = false;
Debug.Log(moving.ToString());
}
}
.
there’s some ugly code in here I plan on changing, like caching vectors and stuff but I’m just trying to get it to work for now.