Find the max extents visible by camera when it is locked within a square

Sorry the title is not fantastic i could not think of a way to put this.

So essentially I have a 2d hex grid map for my game, let’s say it’s Y co-ord is 0. I have a space looking skybox in place. The hex map is bigger than any screen, ie you scroll to access different areas of it.

I want to create a secondary background layer, lets say at y = -15, of stars, so that the grid map, the secondary layer and the skybox will all appear to move at different speeds. Leading to a 3D effect.

The problem is the map is generated at run time depending on input of size. So i don’t know how big the map will be. I need to essentially make sure that any where that’s visible on the main camera at any time, has the secondary starry layer.

I Intended to take a prefab of a plane with a starry material, and clone it to fill the area required, but i don’t know how to find the top-left and bottom-right points of the world.

By this i mean, if the camera is as far up and left as it can go (while locked to the game grid), how do i find the x and z co-ords of the point at the top left of the viewport. And then the same for if the camera was to scroll down to the bottom right.

just to verify, screen to world point is not what i am looking for (at least i don’t think that will work). I need to know the maximum extents that the camera can see by moving around it’s limits.

I’ve read you question more than twice. I’m still fuzzy about what you want. Let me restate what I think you want. You have a top down game view. The camera is parallel to the playing surface. The playing surface (with a ‘Y’ value of 0) is dynamically created at runtime. You have already solved the problem of allowing the user to scroll around but preventing the camera from leaving the playing surface. The playing surface can be seen through, and you want to create a background at a different ‘Y’ value the exact size needed so that if the user is scrolled to any of the min/max extents extents the background is seen.

There is probably some slick mathematical way to solve this problem. I’m going to suggest a bit more brute force approach. Place the camera at one of the horz/vert max extents. That is, so that the corner can just be seen. Then use viewport coordinates and Camera.ViewportToWorldPoint() to calculate the point. If you game board is centered on the origin, then you only have to calculate one point and you can derive the other three. Note the user never sees the camera at that position…you just move the camera, do the calculation, and immediately restore the position.

Viewport coordinates start in the lower left of the screen at (0,0) and go to the upper right at (1,1), so it is easy to specify whichever corner(s) you place the camera. Say you place the camera in the upper left corner. The viewport coordinate will be (0,1).

In making the conversion between viewport coordinates and world coordinates, the ‘z’ specifies the distance in front of the camera. This is a plane to plane distance, so if your camera was at y = 10, and your stars were at y = -15, the ‘z’ parameter would be 25.

So with the camera positioned in the upper left corner, you would do something like:

var upperLeftCorner = Camera.main.ViewportToWorldPoint(Vector3(0,1,25));

And ‘upperLeftCorner’ would be the world coordinate for the upper left corner for your field of stars. If the game board is not centered on the origin, then just move the camera to the opposite corner and repeat the calculation to get another corner. The two points will give you size and position. If it is centered, then the size will twice the abs() of x and z.