- Home /
Unity 2D Camera Player Boundary
I need help with creating a script that is universal for all mobile devices, Android, iPhone, iPad etc.... With all devices screen sizes change therefore the area of play changes. I need a new script to add to my player or camera that will not let the player leave or escape my screen for all screen resolutions. How would I do this? Right now my camera does not move and it will not move, the player only moves in the y direction up/down but it can escape my screen which is not good for any game. Thanks in advance for the help.
Answer by yurypn · Feb 10, 2018 at 07:31 PM
Hi! Use https://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html to get height of boundaries. Camera.orthographic - is half of screen height in world units. And use https://docs.unity3d.com/ScriptReference/Camera-aspect.html to calculate width. More details you can find here https://answers.unity.com/questions/230190/how-to-get-the-width-and-height-of-a-orthographic.html Hope it helps you )