Can't get touch position with the Input System

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?

1 Like

Hi @Vefery,
There are a few points I want to clarify:

  • I used “Release only” interaction which seemed the most fitting for my purpose

This is probably not what you desire, if you need to read out the start position of the swipe this start position is on Press (when the finger first touches the screen), then when the finger is released you want to access the last position in addition, so there are two different values from two different action states you need to access. Press and Release defines that you need to Press and Release a finger on the screen in order to perform an action. Putting it simple: if you want to swipe you need to put your finger on the screen first and in the end release it to finish the action.

Please try the setup with press and release.

  • I need to need to calculate delta position of the swipe-like motion.

Using the mouse position is probably also not what you want to use in this case. Why don’t you use value / touchscreen / delta? This value will be zero when no movement happens, whereas mouse position always has a value.

This example maybe helps understanding the particularities: New Input System - Mouse Press and Hold - Drag And Move (it is a mouse drag but works very similar for touch)