- Home /
Input system get Vector2 mouse position by click
Can someone tell me how to get mouse position by click with new unity input system. I`m able to get mouse position but it updates every time position changes and i need to get value only by click.
When you click (e.g Input.Get$$anonymous$$ouseButtonDown(0)
), set a variable to the Input.mousePosition
at that point. This way, it won't change constantly. Only when you click.
Answer by axelforsberg1 · Jan 11, 2021 at 02:13 PM
This code prints the mose position on click.
void Update()
{
Vector3 mousePos;
mousePos.x = Input.mousePosition.x;
mousePos.y = Input.mousePosition.y;
if (Input.GetMouseButtonDown(0))
{
print(Mathf.Atan2(mousePos.x, mousePos.y) * transform.position);
}
}
}
Answer by TimBur · Jan 09, 2021 at 04:10 PM
First, in your .inputactions asset, create a new action and bind it to the mouse click:
Next, if you don't already have one, add a PlayerInput component to a GameObject in your scene. Set the PlayerInput component to use your inputaction asset. Set Behavior to "Use Unity Events". Then click the little triangle to open the dropdown list of events, and add a custom function to the Click event:
Lastly, put some logic in your click handling function. You know how to get the mouse position, so in your click handling function, write the code to get the mouse position, and then whatever other code you need.
Consider reviewing the documentation on actions and action maps. This is, IMHO, the most useful feature of the new input system. Documentation is here: link
Your answer
Follow this Question
Related Questions
How do I override or place inputs in the input object for automation? 3 Answers
Camera Screen to world point returns cameras transform position. 8 Answers
Simulate mouse input 1 Answer
How can I achive faster response time from Input? 3 Answers
[FULL-SCREEN] INPUT.MOUSEPOSITION OUTPUT DOES NOT MATCH THE DISPLAYED VIEW ON THE SCREEN 0 Answers