Before updating to Windows 10 you may need to return your Pro license. More information here

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, 2010 at 10:48 PM

avatar image

6.5k 253 231 304

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

Sep 26, 2010 at 11:06 PM Marowi

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

Feb 28, 2011 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, 2011 at 02:36 AM

avatar image

97.5k 57 301 529

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

Feb 28, 2011 at 09:47 PM e.bonneville

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

Aug 16, 2012 at 04:37 PM Jim Groth

How would I be able to make it do something different depending on if it's scrolling up or down?

Jul 15 at 03:38 PM Unnatural Selection

GetAxis returns the "delta" value. So a number that represents how the axis has changed in relation to the last frame. This "change" can be positive or negative which usually relates to scroll up or down.

 // C# or UnityScript
 var d = Input.GetAxis("Mouse ScrollWheel");
 if (d > 0f)
     // scroll up
 else if (d < 0f)
     // scroll down

Jul 16 at 02:20 PM Bunny83
(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, 2010 at 11:06 AM

avatar image

372 26 23 35

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, 2010 at 02:19 PM e.bonneville

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

Nov 20, 2012 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, 2010 at 10:48 PM

Seen: 43731 times

Last Updated: Jul 16 at 02:20 PM