Implementing the ScrollWheel:

How would I implement the `ScrollWheel`? I've got a vague idea of using the `ScrollWheel` to make a zoom function in an FPS game. I have a zoom of sorts working, where you press ALT and the field of view decreases, but I would like to use the `ScrollWheel` instead. Thing is, I don't know how to get scroll events, which might hinder my development. If I could get the scroll amount, I could add or decrease the field of view until it reaches a zoom limit. In theory, that should work. Any and all help appreciated.

Thanks in advance - Elliot Bonneville

devilkkw's answer shows the right thing, but saying "simple code" and then post a script with 98% unrelated stuff is not really a good answer.

What you actually need, i guess you've found it already, is `Input.GetAxis("Mouse ScrollWheel")`. It returns the delta movement that happend in the last frame. It can be positive or negative (up/down).

Make sure that the axis is setup in the input manager and named accordingly. The default "Mouse ScrollWheel" is of type "Mouse Movement" and uses the 3rd axis which is normally the scrollwheel.

int travel;
int scrollSpeed = 3;

    void Update()
    {
        var d = Input.GetAxis("Mouse ScrollWheel");
        if (d > 0f && travel > -30)
        {
            travel = travel - scrollSpeed;
            Camera.main.transform.Translate(0, 0, 1 * scrollSpeed, Space.Self);
        }
        else if (d < 0f && travel < 100)
        {
            travel = travel + scrollSpeed;
            Camera.main.transform.Translate(0, 0, -1 * scrollSpeed, Space.Self);
        }
    }

This code worked best for my purposes. It’s a simple forward/backward movement with limits. The limits are relative to the camera’s original position.

Just to add to the answer given by @Bunny83
The output of the Input.GetAxis(“Mouse ScrollWheel”) would be
= n * sensitivity
// n is number of times the wheel is rotated in snaps/ It can be positive or negative
// sensitivity is defined in the Input Settings. Default is 0.1f

Other remarks: You can rename the name “Mouse ScrollWheel” to something else in the Input settings.
For that you have to select MouseMovement as Input type and choose ThirdMovement

this is simply code to do:

    var target : Transform;
var distance = 10.0;

var xSpeed = 100.0;
var ySpeed = 100.0;

var yMinLimit = 2;
var yMaxLimit = 80;

var distanceMin = 3;
var distanceMax = 25;

private var x = 0.0;
private var y = 0.0;

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

    if (rigidbody)
        rigidbody.freezeRotation = true;
}

function LateUpdate () {
    if (target) {
        x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

       y = ClampAngle(y, yMinLimit, yMaxLimit);

        var rotation = Quaternion.Euler(y, x, 0);

        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);

        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;

        transform.rotation = rotation;
        transform.position = position;

    }

}

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);
}

attach on our camera and select target.