- Home /
Unity mouse input gradually becomes off
I am getting the location of the mouse using the following:
Camera.main.ScreenToWorldPoint (new Vector2 (Input.mousePosition.x, Input.mousePosition.y))
I am then instantiating a gameobject using the vector that is returned by the above line. However, after multiple clicks the position that this line returns fails to represent where the user actually clicked. It returns the wrong value.
What am I doing wrong? this is a 2d project, and is written in C#.
SOLUTION:
It seems that having a rigidbody2d attached to the camera caused this behavior, though I have no clue whatsoever why this would be the case. If someone wants to enlighten me as to why this occurs then they are more than welcome.
You need to be specific about what you mean by 'wrong value.' In addition, the code above will behave differently on an Orthographic camera than a Perspective camera. In either situation, setting the 'z ' in a Vector3 to a distance in front of the camera plane will help the situation:
Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
pos = Camera.main.ScreenToWorldPoint(pos);
This will create a position under the mouse 10 unites in front of the camera plane.
I mean exactly what it sounds like, the vector that I am getting is not where I am clicking the mouse. The following is the output from this line of code:
Debug.Log (Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,10f))); target = oldTarget;
and clicking the mouse in the exact same position every time.
What I hadn't noticed before however, is that it is only the Y value that appears to be changing
And no, this is not a mouse issue, as it occurs with my trackpad as well
And just so you know, it is an orthographic camera in a 2d project
Look at the camera position of the main camera while you are doing the clicking. Is it changing?
Your answer
Follow this Question
Related Questions
Detecting mouse click on GameObject with any mouse button. 2 Answers
Raycast hit not detected on cube 1 Answer
Disable mouse input and cursor in game 2 Answers
Mouse Over & Mouse Click 1 Answer