Get mouse/touch position on click/touch with Unity's new Input System
If you need to get the mouse or touch position on click with Unity's new Input System, many solutions suggest making two actions, one for click and one for the position. However, this is wrong and sometimes leads to the error that the first read position value is 0/0.
The correct setup is that you create one action with Action Type Value
and Control Type Vector2
. Then, you click the little plus button and select "Add Binding with one modifier".
For "Modifier" you use either mouse click or touch contact, depending on what kind of input you need. The modifier is the thing that will actually trigger the action.
For "Binding" you use either mouse position or touch position. The binding defines the value of the action whenever the modifier is executed.
In the code, you can use ReadValue<Vector2>
to get the actual position.