- Home /
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.
Answer by robertbu · May 26, 2013 at 03:22 PM
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.
Great answer! Could you recommend a different solution than using colliders? I need to keep the camera within boundaries that I can define myself for each level.
Do it mathematically. That is, with the Viewport conversion you have the boundaries of the camera in world space. I assume you have $$anonymous$$X, maxX, $$anonymous$$Y, maxY for the boundaries for each level. Each time the camera is moved, just check that it does not go past those boundaries. The same is true of zoo$$anonymous$$g. After you change the Field of View or move the camera, calculate the new viewport coordinates. If the positions overlap any of the $$anonymous$$/max values, move the camera.
The issue is that the levels will contain many boundaries. I'm going for a $$anonymous$$ega$$anonymous$$an type level layout. The camera needs to stay within the level where the boundaries are sporadically placed.