- Home /
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?
The support is freely provided by the community.
If you want support from Unity Technologies feel free to subscribe to their premium support.
Answer by YienHoe · Nov 02, 2021 at 09:18 AM
@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
Answer by msanatan · Aug 19, 2021 at 01:32 AM
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
@yanerobot don't upgrade to 2021, all the screen coordinates are now (Infinity, Infinity). I can't believe it got worse lol
Answer by Ercova · Aug 19, 2021 at 07:04 AM
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 did your set up, with the exception of mouse pointers:
Here's the input action for pressing: https://ibb.co/98fr3qn
And the input action for position: https://ibb.co/bLL8vTg
And these are my logs of screen and world coordinates: https://ibb.co/JxKnWYR
When I read the value, I always get (0, 0)
, like the OP.
I realized the bug happens when the binding is Primary Touch/Touch Contact. It picks up the right position when I set it to Touch 0/Touch Contact, or event Primary Touch/Tap. It's just Primary Touch/Touch Contact that's not working. Give it a try @yanerobot
Answer by imaxs · Aug 26, 2021 at 04:35 PM
I think this should help you:
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.
Answer by davidhernandez · Mar 04 at 05:23 AM
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
Your answer

Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
How to distinguish between two touch screens for input in android 0 Answers
When pressing a button, it counts as pressing the screen and the button [New Input System] 0 Answers
How do you get position from fingerId 1 Answer
How can we make touch controls like Hole.io/ Bumper.io? 0 Answers