- Home /
How to make something happen when mouse on side of screen?
I would like it so when the mouse is on the left side of the screen something happens and when it's on the other side something else happens. Can someone help or link me a website to help?
Thanks in advance
Answer by robertbu · Nov 20, 2014 at 09:44 PM
You can use Viewport coordinates for this. Viewport coordiantes start at (0,0) in the lower left and go to (1,1) in the upper right:
var viewportCoord = Camera.main.ScreenToViewportPoint(Input.mousePosition);
if (viewportCoord.x < 0.05) {
Debug.Log("Mouse on the left edge of the screen");
}
if (viewportCoord.x > 0.95) {
Debug.Log("Mouse on the right edge of the screen");
}
Adjust the 0.05 and 0.95 depending on how much of the edge you want to use. If you want the mouse to fire a single event, you'll have to add some state that detects entering and leaving the edge zones.
@robertbu great thanks! I will try and learn a bit about view port coordinates and what's in this answer before I try and use it :P
Your answer
Follow this Question
Related Questions
Detect cursor on edge of screen 1 Answer
How to detect in which half of the screen the cursor is, no matter the resolution? 1 Answer
How to stop mouse getting out of the game, my game is in webplayer? 2 Answers
Unity 3D Screen.lockCursor Problems 2 Answers
Disable mouse input and cursor in game 2 Answers