How to convert mouse position to world coordinates

I have a 2d unity project. I cannot depend on “OnMouseExit” because overlapping 2D box colliders cause the meathod to trigger even when the mouse is inside the bounds just bacause something else is in front (which is not my intention)

I was going to manually check for the mouse exiting on every frame by using:


But this does not work because ‘mouse position’ is in terms of the number of pixels across the screen, and ‘bounds’ is in terms of “Units” relative to the origin of the scene. The camera Authographic and slides around to look at the 2D plane that the world’s sprites sit on. I have no idea how many “units” fit across the screen and suspect that it would change as soon as you change the aspect ratio or screen size.

There are multiple methods of Camera which will help you to change coordinates of object.


Camera documentation