I need to need to calculate delta position of the swipe-like motion. For this I set up my action like this:
I used “Release only” interaction which seemed the most fitting for my purpose (I also tried playing around with Tap and Slow Tap with no success). To my understanding it triggers “started” event on touch start and “performed” event on touch end. I use the action in code like this and “performed” is never triggered (but it is triggered without interaction).
void Start()
{
jumpAction = InputSystem.actions.FindAction(jumpActionName);
jumpAction.started += HandleTouchAction;
jumpAction.performed += HandleTouchAction;
jumpAction.canceled += HandleTouchAction;
}
private void HandleTouchAction(InputAction.CallbackContext context)
{
if (context.started)
{
touchStartPos = context.ReadValue<Vector2>();
}
else if (context.performed)
{
Vector2 newPos = context.ReadValue<Vector2>();
Vector2 delta = newPos - touchStartPos;
if (delta.x > swipeDeltaThreshold)
Debug.Log("Right");
else if (delta.x < -swipeDeltaThreshold)
Debug.Log("Left");
}
else if (context.canceled)
{
touchStartPos = Vector2.zero;
}
}
I also tried to use this setup:
Here at least the “performed” event is actually triggered. But there’s another problem - in both “performed” and “canceled” ReadValue always returns (0,0). But in “started” event I get actual screen coordinates.
What am I doing wrong?