- Home /
Detect cursor on edge of screen
I'm not working on anything that needs this script right now but I am curious, does anyone know a script for detecting when the mouse hits the edge of the screen? Examples of the script I'm looking for are found in top-down strategy games like Civilization 5 and Starcraft 2. I'm not looking for the camera scrolling mechanic itself, just the code to detect the mouse on the edge of the screen.
Answer by $$anonymous$$ · May 11, 2013 at 06:15 PM
You can use Input.mousePosition to detect where is your cursor http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html
For example:
public class RTScamera : MonoBehaviour
{
public float ScrollSpeed = 15;
}
// Update is called once per frame
void Update()
{
if ( Input.mousePosition.y >= Screen.height *0.95)
{
transform.Translate(Vector3.right * Time.deltaTime * ScrollSpeed, Space.World);
}
}
}
I see, that can be very useful. I'll keep this script in $$anonymous$$d for future reference, thanks.
Your answer
Follow this Question
Related Questions
How to make something happen when mouse on side of screen? 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
How can I move the camera when the mouse reaches the edge of the screen? 3 Answers
Move the camera when the mouse reaches the edges of the screen 1 Answer