• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Celldweller02 · Jan 08 at 03:08 PM · inputinput.mouseposition

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Jan 09 at 02:19 PM 0
Share

When you click (e.g Input.GetMouseButtonDown(0)), set a variable to the Input.mousePosition at that point. This way, it won't change constantly. Only when you click.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by axelforsberg1 · Jan 11 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);
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by TimBur · Jan 09 at 04:10 PM

First, in your .inputactions asset, create a new action and bind it to the mouse click: alt text

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: alt text

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


clickaction.png (28.2 kB)
playerinput.png (39.8 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input.mousePosition incorrect on second monitor 0 Answers

[FULL-SCREEN] INPUT.MOUSEPOSITION OUTPUT DOES NOT MATCH THE DISPLAYED VIEW ON THE SCREEN 0 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Baffling Input.Mouseposition problem. If statement not correctly working. 1 Answer

Mouse drag horizontally and vertically is not working..? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges