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

more ▼

asked Mar 19 '10 at 10:48 PM

e.bonneville gravatar image

6k 104 120 165

Did you ever get this solved? The curse of getting an answer, even if it's not the one you're after...

Sep 26 '10 at 11:06 PM Marowi

I finally did. Yeah, it's awful. :)

Feb 28 '11 at 09:48 PM e.bonneville
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Feb 26 '11 at 02:36 AM

Bunny83 gravatar image

67.2k 22 69 269

Yup, that's what worked for me in the end. Thanks for answering!

Feb 28 '11 at 09:47 PM e.bonneville

Works like a charm. Got myself a complete orbit camera now. :)

Aug 16 '12 at 04:37 PM Jim Groth
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 20 '10 at 11:06 AM

devilkkw gravatar image

317 9 10 19

Nope. That isn't doing it - I'm not sure what that actually is doing... Is it supposed to zoom in on a target? I think I've been misunderstood. Thanks for the reply anyway, though.

Mar 20 '10 at 02:19 PM e.bonneville

do you know how to keep the zoom value in that??

Nov 20 '12 at 03:54 PM kifcaliph
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 19 '10 at 10:48 PM

Seen: 24027 times

Last Updated: Nov 20 '12 at 03:54 PM