- Home /
Baffling Input.Mouseposition problem. If statement not correctly working.
Okay. So in the scripting API for Input.MousePosition it says:
"Note: Input.mousePosition reports the position of the mouse even when it is not inside the Game View, such as when Cursor.lockState is set to CursorLockMode.None. When running in windowed mode with an unconfined cursor, position values smaller than 0 or greater than the screen dimensions (Screen.width,Screen.height) indicate that the mouse cursor is outside of the game window."
So I came up with this code which in my head is supposed to move an object "looker" left and right, and up and down, based on the mouse input axis, but only while the cursor is on the screen. There are no debug errors, debugging the mouse position even showing negative numbers, the lines inside the statements are still happening. - the object is still being transformed even when the mouseposition is showing as being off the screen. Why?
if (Input.mousePosition.y >= 0f & Input.mousePosition.y <= Screen.height) {
//storepos2.position += new Vector3(v,h, 0);
looker.transform.position = new Vector3 (gameObject.transform.position.x, storepos2.position.y, gameObject.transform.position.z);
}
if (Input.mousePosition.x >= 0f & Input.mousePosition.x <= Screen.width) {
//storepos2.position += new Vector3(v,h, 0);
looker.transform.position = new Vector3 (storepos2.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
}
Answer by ASPePeX · Feb 27, 2017 at 09:46 AM
To check if the mouse pointer is actually inside the window you have to evaluate both conditions at once (x and y position).
if (Input.mousePosition.y >= 0f && Input.mousePosition.y <= Screen.height && Input.mousePosition.x >= 0f && Input.mousePosition.x <= Screen.width)
{
Debug.Log("Got it!");
}
Your answer
Follow this Question
Related Questions
have object rotate to the mouses screen position 1 Answer
Mouse movement swings character around in circles 1 Answer
Left Click While Holding Right Click 1 Answer
How to disable MouseLook in ingame pause. 1 Answer
MouseY always enabled? 3 Answers