- Home /
Need to limit zoom in/out of camera script
Hello
i have this very simple camera scripe
var cam : Camera;
function Update(){
cam.fieldOfView += 5 * Input.GetAxis("Mouse ScrollWheel");
}
I need to limit the max zoom in and out of the camera.
If someone can point me in the right direction, on how i would go about doing this.
Thanks in advance
Answer by LukaKotar · Feb 02, 2013 at 02:05 PM
var cam : Camera;
var min : float; //Min FOV (zoomed in)
var max : float; //Max FOV (zoomed out)
function Update(){
cam.fieldOfView += 5 * Input.GetAxis("Mouse ScrollWheel");
//Use Mathf.Clamp to keep the value always lower than 'max' and greater than 'min'
cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, min, max);
}
Answer by Ut1lityDev · Dec 13, 2020 at 09:33 AM
Here is the C# version of @LukaKotar's script.
public Camera camera;
float min;
float max;
void Update() {
cam.fieldOfView += 5 * Input.GetAxis("Mouse ScrollWheel");
cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, min, max);
}
Answer by joeyaaaaa · Jul 29, 2012 at 11:24 PM
var distanceMin = 4; var distanceMax = 45;
function LateUpdate () { if (target) { distance = Mathf.Clamp(distance - Input.GetAxis("Camera Zoom")5, distanceMin, distanceMax); if (Input.mousePosition.x > Screen.width - 2 || Input.mousePosition.x < 2 || Input.mousePosition.y > Screen.height - 2 || Input.mousePosition.y < 2) DoIT = 1; else DoIT = 0; if (FirstLogin == 0) { FirstLogin = 1; DoIT = 1; } if (DoIT == 1) { if (Input.mousePosition.x > Screen.width - 2) x += 60 xSpeed 0.02 Time.deltaTime; if (Input.mousePosition.x < 2) x -= 60 xSpeed 0.02 Time.deltaTime; if (Input.mousePosition.y < 2) y += 30 ySpeed 0.02 Time.deltaTime; if (Input.mousePosition.y > Screen.height - 2) y -= 30 ySpeed 0.02 * Time.deltaTime; y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0); var position = rotation * Vector3(0.0, 0.0, -distance) + target.position; var hit : RaycastHit; if (Physics.Linecast (target.position, transform.position, hit)) { distance -= hit.distance; }
transform.rotation = rotation;
transform.position = position;
lastPos = transform.position - target.position;
}
else transform.position = target.transform.position + lastPos;
} }
static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }
function StartTheCam() { distance = Mathf.Clamp(distance - Input.GetAxis("Camera Zoom")5, distanceMin, distanceMax); var angles = transform.eulerAngles; x = angles.y; y = angles.x; y = ClampAngle(y, yMinLimit, yMaxLimit); var rotation = Quaternion.Euler(y, 0, 0); var position = rotation Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation; transform.position = position; lastPos = transform.position - target.position; transform.position = target.transform.position + lastPos; }
i dont really know how to do it but this works but the zoom doesnt show it has worked till you move the camera again. it mightjust be in the wrong spot lol
Answer by Ingen · Jul 30, 2012 at 01:20 AM
maybe something like this?
pseudo code not expect can work at now, just to have a line of think. Hope can help
var cam : Camera;
var Dist : Float;
var minDist : Float;
var maxDist : Float;
var zoom : boolean;
function update
if Dist<=minDist = false
if Dist>=maxDist = false
if {
input scrollMouseDown
Zoomin;
}
if {
input scrollMouseUP
ZoomOut;
}
function ZoomIn (){
cam.fieldOfView += 5
}
function ZoomOut (){
cam.fieldOfView -= 5
}
good look
Your answer
Follow this Question
Related Questions
Scoping script /Camera zoom preferably C# 3 Answers
Camera Zoom Input change. 1 Answer
How to move camera on local z axis 2 Answers
Changing camera position to shoot through scope on gun 1 Answer
Raycast misses after camera zoom 2 Answers