- Home /
Mouseclick landing of screen
Im using this code to move my player to where the mouse is clicked. It has been working great. But at some point something happened and now the debug line shows the mouseclick to be Waaaaay of the screen (and my player moves there). I have not done anything to this code. I have stripped the project of everything but one gamobject with this code and still it produces these values:
print (string.Format ("MousePosition {2} - Camera: {0} - Target: {1}", camera.transform.position, target, mouseClick));
MousePosition (2189.0, -75.0, 10.0) - Camera: (0.0, 0.0, -10.0) - Target: (507.1, -56.3, 0.0)
MousePosition (2309.0, 90.0, 10.0) - Camera: (0.0, 0.0, -10.0) - Target: (542.4, -7.8, 0.0)
MousePosition (2831.0, 71.0, 10.0) - Camera: (0.0, 0.0, -10.0) - Target: (695.8, -13.4, 0.0)
if (Input.GetMouseButtonDown (0)) {
var mouseClick = Input.mousePosition;
mouseClick.z = 0 - camera.transform.position.z;
var target = camera.ScreenToWorldPoint (mouseClick);
Debug.DrawLine (camera.transform.position, target, Color.red, 10f);
print (string.Format ("MousePosition {2} - Camera: {0} - Target: {1}", camera.transform.position, target, mouseClick));
}
Answer by Lautaro-Arino · Feb 11, 2014 at 08:37 AM
I solved it. Quite a bit embaresing. It turns out that Unity lets you open more then one Game window. And if you have one in a tab that you are not aware of and you use a second one and click it the first one is still used as the main game. Therefore your mousecliks are registered way of the screen you are clicking in.
So dont have more then one game window open!
I hope this can help someone else.