# Limiting Camera Movement

 0 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. more ▼ asked Sep 03 '10 at 03:50 AM Josh 7 38 ● 6 ● 6 ● 12 I'm assuming that the camera script is attached to some sort of player object, and the zoom in/out is setting the relative position of the camera to that object..? Sep 03 '10 at 04:20 AM Marowi add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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. more ▼ answered Sep 03 '10 at 04:18 AM Marowi 4.9k ● 4 ● 14 ● 53 unfortunately the mouselook script is in C# and I really wanted everything to be in JavaScript, but I suppose it shouldn't be to hard to convert. I'll give it a shot.Thanks a lot for all your help. Sep 03 '10 at 01:26 PM Josh 7 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x3446
x2988
x721
x182
x20

asked: Sep 03 '10 at 03:50 AM

Seen: 2723 times

Last Updated: Sep 03 '10 at 03:50 AM