all the answers i found are from 2012, and the code its not working anymore, can someone pls answer this question? its a 2dgame with a chesslike view but the board its bigger(its my poor attempt to an RTS) i pretty much am a noob at code so I have no idea how to do this
Code from 2012 should still work for this, maybe with minor changes. Completely scrapping what you’ve found might not be the best way.
Anyway, you get the mouse position with Input.mousePosition, and the game window size with Screen.width and Screen.height. The lower-left corner of the game window is 0,0. All values are in pixels, so if you want only the outermost pixel to count, you can simply do
var mousePosition = Input.mousePosition;
if (mousePosition.x <= 0)
{
Debug.Log("Left");
}
else if (mousePosition.x >= Screen.width - 1)
{
Debug.Log("Right");
}
if (mousePosition.y <= 0)
{
Debug.Log("Down");
}
else if (mousePosition.y >= Screen.height- 1)
{
Debug.Log("Up");
}
And replace the Debug.Logs with camera movement code in the matching direction.
We’re not using ==, but >= and <= so it still works in windowed mode, if the cursor leaves the game window.
If you want to say “the outermost 5% of the screen count”, you can divide the mouse position components (x and y) by the screen dimensions to get a Vector that goes from 0,0 to 1,1 instead:
var viewportMousePosition = new Vector2(mousePosition.x / Screen.width, mousePosition.y / Screen.height);
and then check whether x or y is <= 0.05f or >= 0.95f.