I am spawning a dropdown menu when the mouse clicks over certain objects. This works for the most part however, in certain locations on the screen if you click the menu option extends past the edge of the screen. like in the image bellow.
I was thinking of checking to see if the game object is outside the bounds of the edge of the camera. However, from my understanding this would take it from the center of the GameObject instead of the edge no? and it would not tell me how far it extends past the edge of the camera. Is there a better way of doing this or is this way the best way that can be achieved?
you can do
camMax =Camera.transform.position.x + Camera.main.ortiographicSize * Screen.width / Screen.height;
for the camera max x bound
and then put a collider on the object and check the bounds.max.x
then if you do the obj.bounds.max.x - camMaxX you will have the distance it is offscreen
Once the player clicks somewhere, you need to check if the x position is less then the edge of the screen minus half the width of the menu (and opposite for the left side)…
Vector2 requestedMenuPosition = Camera.main.ScreenToWorldPoint( Input.mousePosition); //THE POS OF THE MOUSE WHEN CLICKED
float widthOfMenu = 3; //????
Vector2 screenDimmensions = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height)); //THE DIMMENSIONS OF THE SCREEN IN WORLD CO-ORDS
if (requestedMenuPosition.x < screenDimmensions.x - (widthOfMenu/2) || requestedMenuPosition.x > -screenDimmensions.x + (widthOfMenu / 2))
{
//GOOD
//THEY CLICKED IN SPOT WHERE THE MENU WOULD STAY ON SCREEN
} else
{
//BAD
//THEY CLICKED IN A SPOT WHERE THE MENU WOULD GO OFF SCREEN
}
The real question is how do find the width of your menu? It will change depending on screen sizes and it’s in a Canvas which uses a different co-ordinate system. Maybe there’s a way but I might simply check if the mouse is, say, a quarter of the screen’s width away from the edge.