Clamp Mouse Zoom using Scroll Wheel (SOLVED)

Hello, I’m making a RTS game and i created a little line of code to control mouse zoom
here’s the code.

var ROTSpeed = 10f;
function Update()
gameObject.transform.Translate(0,0,Input.GetAxis("Mouse ScrollWheel") * ROTSpeed);

So that’s My code, it works just fine. Perfectly in fact, but I have one problem i can zoom in and out infinitely. Does anyone know how i could clamp the zoom? Thanks in advance! :smiley:

Store the amount in a variable, then clamp it.
For example (UnityScript):

var ZoomAmount : float = 0; //With Positive and negative values
var MaxToClamp : float = 10;
var ROTSpeed : float = 10;

function Update() {
    ZoomAmount += Input.GetAxis("Mouse ScrollWheel");
    ZoomAmount = Mathf.Clamp(ZoomAmount, -MaxToClamp, MaxToClamp);
    var translate = Mathf.Min(Mathf.Abs(Input.GetAxis("Mouse ScrollWheel")), MaxToClamp - Mathf.Abs(ZoomAmount));
    gameObject.transform.Translate(0,0,translate * ROTSpeed * Mathf.Sign(Input.GetAxis("Mouse ScrollWheel")));


For those that still need an answer to this, using Mathf Clamp is not the best way. Instead, simply write the following code on Update()

    if (Input.GetAxis("Mouse ScrollWheel") > 0)
        if (mycamera.fieldOfView > 1)
    if (Input.GetAxis("Mouse ScrollWheel") < 0)
        if (mycamera.fieldOfView < 100)

This only zooms in if the field of view is greater than the minimum (which is 1 for all cameras). The maximum value is greater than 100 but 100 is a good value.

Ok that script does an excellent job at moving the cam backwards and forward but I have been playing with it for a day now and I cant get it to limit the amount of movement backwards and forward.

Example: forward limit 0.5, backward limit 1

I know a little bit about scripting I just wouldn’t know what to use to accomplish this.

else?, Bool?, <>=?, new float var?

I’m so lost right now.