I’m trying to wrap my head around where to start with this so please feel free to point me to tutorials or even Asset Store solutions as you see fit.
The best way to describe what I want is like the Angry Birds camera since everyone knows that game well though there are other good examples like Worms. I’m making a 2D game and I want you to be able to zoom the camera in and out. However if the camera started on the left hand of the level, I want the camera to “bump” against the side and only zoom out to the right as to not show the empty space off screen.
I also want the camera to be able to zoom out until the point where it is showing the entire level from horizontally. Again it would bump against the closest side and then zoom the other direction until it hit that side and stop.
So basically I want to put some sort of maximum bounds that the camera can zoom out on the left and right side.
Oh and I’m using a Perspective Camera if that makes a difference.
you want to adjust the camera position and distance, based on the outermost object to be in view, and the horizontal angle of the camera frustrum.
have tried to solve this using some trig, but the horizontal FOV is not working properly (hopefully someone else can help!).
Theory is : Tan Theta = Opposite / Adjacent .: Adjacent = Opposite / Tan Theta . Theta is half of the horizontal FOV angle, Opposite is half the distance between the outermost objects.
Here is my script. I attached it to the camera, then created a sphere and a cube. Dropped the sphere and cube in the Inspector (for leftmostObject and rightmostObject). Hit play and move the cube or sphere. Works for X and Y axis, breaks moving on Z axis.
#pragma strict
public var leftmostObject : Transform;
public var rightmostObject : Transform;
function Update()
{
var LeftEdge : Vector3 = leftmostObject.position;
var RightEdge : Vector3 = rightmostObject.position;
var HalfView : Vector3 = (RightEdge - LeftEdge) * 0.5;
var CentreOfView : Vector3 = LeftEdge + HalfView;
// not working properly ? but close !
var calcAngle : float = ( Camera.main.fieldOfView / Camera.main.aspect ) * 0.5;
var camPosZ : float = 0 - ( HalfView.x / Mathf.Tan(calcAngle) );
transform.position = CentreOfView + Vector3(0, 0, camPosZ);
}