I’m working on a top down space shooter that I want to release for multiple platforms. I must keep a large number of aspect ratios into account. Therefore I find want a simple way to limit the movement of the character to the visible area of a camera.
Does anybody have an idea or at least a starting point on how this can be done?
You can find the world coordinates of the bottom/left and top/right points with Camera.main.ViewportToWorldPoint. The argument required is a Vector3, where x and y are the viewport coordinates (0 to 1), and z is the distance from the camera. You can pass any value as z if your camera is in orthographic mode and looking to one of the axes - the typical top down game settings (if you’re using other settings, change to these ones, for the sake of your mental sanity)
var top: float;
var bottom: float;
var left: float;
var right: float;
var topRight = Camera.main.ViewportToWorldPoint(Vector3(1, 1, 0));
var bottomLeft = Camera.main.ViewportToWorldPoint(Vector3(0, 0, 0));
top = topRight.y; // topRight.z if your camera is looking in -y direction
right = topRight.x;
bottom = bottomLeft.y; // bootmLeft.z, if camera in -y direction
left = bottomLeft.x;