- Home /
How to obtain mouse position relative to Transform.position
I want to get the position of the mouse when the user clicks a button in the game. However the position I get using Input.mousePosition is in pixels and it starts at the bottom left corner. The staring point is "fixable" by substracting it from Screen.Width and Screen.Height, but I need the position I get not to be in pixels but to be in the same scale I use to move GameObjects (ie. gameObject.Transform.Position != Input.mousePosition fixed). How can I convert pixels to that scale?
I should note that my game is a 2D game, so the only relevant axis are X and Y.
Thanks in advance.
Answer by flaviusxvii · Oct 08, 2013 at 07:01 PM
mousePosition isn't just in pixels.. it's in another Vector space called Screen Space.
The position of a gameObject.transform is in World Space, so you are comparing apples and oranges. You can get the position of the mouse projected into World Space.
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
Your answer
Follow this Question
Related Questions
Scale i rotate by mouse position 0 Answers
Gradually scale platform? 4 Answers
GUI Texture scale and position according to the actual screen resolution.. 11 Answers
Can the insides of a gameObject affect its transform? 0 Answers
Unity scale/units and mipmaps 2 Answers