Hi guys,
I’m using the “new” input system, and I’m trying to get an OnMove event to continuously fire if the key binding is held down. I figured the best way to do this would be to have a bool that calls OnMove in Update if true, but in practice I’m not entirely sure how to get it to work, since the actions obviously need a callback context and I’m not even entirely sure what that is. Here’s what I tried:
public class PlayerController : MonoBehaviour
{
[SerializeField] private int moveSpeed = 5;
public bool isMoving;
private Rigidbody2D rb;
private void Awake()
{
rb = GetComponentInParent<Rigidbody2D>();
}
public void OnMoveStart(InputAction.CallbackContext context)
{
rb.velocity = new Vector2(moveSpeed * InputManager.inputActionAsset.Ground.Move.ReadValue<float>(), 0);
isMoving = true;
}
public void OnMoveEnd(InputAction.CallbackContext context)
{
isMoving = false;
}
private void Update()
{
if (isMoving)
{
OnMoveStart(); //obviously this line is not syntactically correct, as it needs a context parameter. rider's advice was to create a new instance of the callback context, which first of all doesn't work,but secondly I don't know how callback context works so it's confusing.
}
}
}
My Input Manager just subscribes the events to the input action asset and enables the action map, here it is:
public class InputManager : MonoBehaviour
{
public static Controls inputActionAsset;
public PlayerController Player;
private void OnEnable()
{
inputActionAsset = new Controls();
inputActionAsset.Ground.Move.started += Player.OnMoveStart;
inputActionAsset.Ground.Move.performed += Player.OnMoveEnd;
inputActionAsset.Enable();
}
}
I made the Input Manager a child of the player which I intend on turning into a prefab to instantiate the player, because this seems like a good way to keep things separate. Thanks for any advice.