# Limiting Camera Movement

I have my script written out for my camera movement. I have "Pan" "Rotate" and "Zoom" functionality.

The problem is the rotation is really awkward. I need the rotation to be similar to an FPS. Zooming should only zoom so far or so close.

My questions are how can I refine my camera rotation and limit how close/far the camera can zoom?

Here is my script:

``````// This script controls camera movement.
//Variables
var horizontalSpeed = 0.5;
var verticalSpeed = 0.5;
var turnSpeed = 2;

function Update ()
{
//"Pan Camera"
if(Input.GetMouseButton(0))
{
var h = horizontalSpeed * -(Input.GetAxis ("Mouse X"));
var v = verticalSpeed * -(Input.GetAxis ("Mouse Y"));
transform.Translate (h, v, 0);
}
//"Rotate Camera"
else if(Input.GetMouseButton(1))
{
h = turnSpeed * Input.GetAxis ("Mouse X");
v = turnSpeed * Input.GetAxis ("Mouse Y");
transform.Rotate (v, h, 0);
}
//"Zoom Camera Out"
else if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
var d = Input.GetAxis ("Mouse ScrollWheel") * 20;
transform.Translate (0, 0, d);
}
//"Zoom Camera In"
else if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
d = Input.GetAxis ("Mouse ScrollWheel") * 20;
transform.Translate (0, 0, d);
}
else
{
}
}

``````

I'm guessing that my only problem is limiting how far the camera can zoom/rotate. Any help is appreciated.

You could Mathf.Clamp to limit the values to a range, or just ifcheck before applying change. Personally, I prefer clamping to be sure of the value range, but your setup would probably work better with ifchecks.

As an example:

``````else if(Input.GetAxis("Mouse ScrollWheel") != 0)
{
if ( transform.position.z >= -20 && transform.position.z <= 0 )
{
var d = Input.GetAxis ("Mouse ScrollWheel") * 20;
transform.Translate (0, 0, d);
}
}

``````

Note that I combined both the zoom in and out into a single ifcheck, as the contents were identical. I then use an ifcheck to make sure that the 'z' position is between -20 and 0 (purely as examples, adapt to your own needs) before allowing the input to translate the position.

I'd also suggest that you changed the elseifs to 'if', otherwise you won't be able to zoom while either mouse button is held (unless that's intended functionality).

Regarding the camera rotation, the MouseLook script (from the standard assets) has a nice rotation setup. Might be worth taking a look at it, and seeing if it's similar to what you're after.