Find camera bounds at certain distance from camera?

Hey everyone. I am currently in the process of creating a 2.5D game that will use a camera similar to classic NES games. The most important constraint for this camera is that it remain within the bounds of the level - think a MegaMan style camera. To accomplish this, I am going to try and place colliders around the bounds of my levels and then place colliders around the edge of the camera’s viewport. When a collision occurs, I know that the camera can not move in that direction anymore. I’m not sure if this implementation will work, but I’m trying to give it a shot. With that said, I need to place colliders at the edges of the camera’s viewport at the z-position of the player. I am having trouble determining these positions.

I want to make this as robust as possible so that I don’t run into issues if I zoom in/out. Additionally, I would like the camera to work on all aspect ratios. I could, of course, manually place these GameObjects to be at the camera bounds and it would work, but that’s too specific.

My first instinct was to make this a triangle problem. I know the distance of the camera to the player and I know the angle of the camera’s field of view. So, if my trig isn’t wrong, I could use (tan(theta) * dist) to find the vertical distance and then (tan(theta * aspectRatio) * dist), where aspectRatio = Screen.width / Screen.height, to get the horizontal distance. Camera.fieldOfView gives the angle of the vertical, the horizontal is affected by the aspect ratio so that’s why I’m multiplying the angle for the horizontal by aspectRatio. This was my first whack at the problem and it didn’t work.

Does anyone have any suggestions?

TL;DR Find view port bounds at a certain distance away from the camera.

I don’t think colliders will work for you given that you want to change the field of view/move camera. Given you know the distance from the camera to the character, you can approach this problem in two ways the first is the one you outline above. Second is to use Viewport coordinates. See answer here for more info on both:

http://answers.unity3d.com/questions/391324/stretching-a-gameobject-to-fit-viewport.html

Note as you change the field of view, you will need to move the camera to keep it in view.