New Input System returns zero Touch position on first call

Hello. This is slice of my code:

using UnityEngine;

public class Swipe : MonoBehaviour
{
    InputMaster input;

    Vector2 startPos;

    void Awake()
    {
        input = new InputMaster();
        input.Enable();
    }

    void Start()
    {
        input.Touch.Tap.performed += ctx => CachePosition();
    }

    void CachePosition()
    {
        startPos = input.Touch.Position.ReadValue<Vector2>();
        print(startPos)  // !prints  "(0,0)" on first call and then prints appropriate values.
    }
}

What I’m trying to accomplish here is to cache screen position of tap on tap (event). And it works fine from the second call onwards. But on the first call it returns Vector.zero (0,0). I don’t get what the problem might be caused by, I’ll try to leave any releavant information here.
These 2 input actions has no interactions or processors.
“Tap” action is button bind to “Primary Touch/Tap [Touchscreen]”.
“Position” action is of type “value” - “Vector2” and pretty self explainatory bind to “Position [Touchscreen]”.

Maybe my input settings are wrong.

Really? There’s no any answer for such an important issue??? Where’s the support?

I’m having the same problems… no solution as yet. I’m using v2020.3.10f1 LTS, hoping that an upgrade to 2021 would stop this issue

Ok, Let me tell the possibilies then.
First => make sure your Tap action is Button and interactions is selected press and release.
and for Bindings for Tap action will be PrimaryTouch/TouchContact? and Left Mouse Button.

Second make sure your Position action is Pass through and Type is Vector2.
and For bindings will be again Primary Touch/Position and Mouse Position.

input.Touch.Tap.canceled+= _ => CachePosition();

I think this should help you:
*
Pressing and position actions
*
Subscribing to events
Note: Subscribing to the “PrimaryTouch.started” event is done after “PrimaryTouchPosition.started” event is called once.

m_InputControllers.Touch.PrimaryTouchPosition.started += (context) => {
      StartPrimaryTouch(context);
      m_InputControllers.Touch.PrimaryTouch.started += context => StartPrimaryTouch(context);
};
m_InputControllers.Touch.PrimaryTouch.canceled += context => EndPrimaryTouch(context);

read positions

private void EndPrimaryTouch(InputAction.CallbackContext context) => Debug.Log(m_InputControllers.Touch.PrimaryTouchPosition.ReadValue<Vector2>());

private void StartPrimaryTouch(InputAction.CallbackContext context) => Debug.Log(m_InputControllers.Touch.PrimaryTouchPosition.ReadValue<Vector2>());

UPD: I forked their repo on github (v 1.1.0), made some fixes and improvements. So now this seems to be fixed, at least for me. Check this out at: GitHub


now it works right (without subscription inside the “PrimaryTouchPosition.started” event)

private void Start()
{
    m_InputControllers.Touch.PrimaryTouch.started += context => StartPrimaryTouch(context);
    m_InputControllers.Touch.PrimaryTouch.canceled += context => EndPrimaryTouch(context);
}


private void EndPrimaryTouch(InputAction.CallbackContext context) => Debug.Log(m_InputControllers.Touch.PrimaryTouchPosition.ReadValue<Vector2>());
    
private void StartPrimaryTouch(InputAction.CallbackContext context) => Debug.Log(m_InputControllers.Touch.PrimaryTouchPosition.ReadValue<Vector2>());

Note: There are no changes in the Input Actions (looks the same as on the screenshot above), but you should add a new component.


Player Input  Component

@yanerobot My solution for this is to make the programme wait until the end of the first frame before calling the function again. Link to the code answer below:

https://stackoverflow.com/a/69807869/15167614

Hope this helps

Still an issue on 2020.3.23 with Input System 1.3.0

It also happens when I disable the Touch input, the first touch event will return a vector2 with zero values