# Smoothing mouse wheel

**URL:** https://discussions.unity.com/t/smoothing-mouse-wheel/403866
**Category:** Scripting
**Tags:** Scripting
**Created:** [December 16, 2009, 7:35pm UTC](https://discussions.unity.com/t/smoothing-mouse-wheel/403866 "2009-12-16T19:35:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_77129819](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@anon\_77129819](https://discussions.unity.com/u/anon_77129819)
#### Post date: [December 16, 2009, 7:35pm UTC](https://discussions.unity.com/t/smoothing-mouse-wheel/403866/1 "2009-12-16T19:35:00Z")

</div>

I’m scripting a ‘[noclip](http://en.wikipedia.org/wiki/Noclip_mode)’ FPS camera controller that can roam/fly freely anywhere without gravity or physics. Sofar the [W]A~~D control works well with the mouse-looking control to fly towards anywhere the camera is facing, and W[A]S[D] input takes care of strafing perfectly.~~  
~~The trouble is with one more feature I wanted to add: **mouse wheel** for climbing and descending vertically (think of a helicopter). Similar to the WASD mouse look movements, i’d need the scrolling up 'n down to be **smoothed**.~~  
~~The [Input Manager docs say](http://unity3d.com/support/documentation/Components/class-InputManager.html) That the ‘Gravity’ (recentering) value is “…Only used when the Type is **key / mouse button** ”, thus excluding mouse wheel.~~  
~~This is quite apparent in my game; when I scroll up/down, the vertical movements _do_ work but its terribly choppy/jerky/skippy. I need it to be smoothed like the WASD.~~  
~~What are the exact calculations needed to implement the lerping / timing / accelerating / deccelerating / etc for this to work? The effect i’m looking for is; when the mouse scrolls (instead of jumping) **gradually accelerate** towards the target direction. When mouse stops scrolling, **gradually deccelerate** the latest vertical movement until full stop.~~  
~~Any help would be greatly appreciated, Thanks~~  
~~kingIZZZY~~

---

<div class="post-metadata">

### Author: ![anon\_742286](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@anon\_742286](https://discussions.unity.com/u/anon_742286)
#### Post date: [December 17, 2009, 12:55am UTC](https://discussions.unity.com/t/smoothing-mouse-wheel/403866/2 "2009-12-17T00:55:16Z")

</div>

Hey,  
after our chat I went to see which program on my computer actually had smooth mouse wheeling.  
Turns out, the only one was Opera, and even that one was a little jumpy. So since this is kind of something special and might be of use for me later, I did my best and came up with this:

```auto
using UnityEngine;
using System.Collections;

public class SmoothMouseWheelTranslation : MonoBehaviour {
    float timer;
    float translation;
    float position;
    float target;
    float falloff;
    float input;

    // Increases the target translation
    public float speed = 100.0f;
    // Maximum acceleration possible by scrolling the wheel faster
    public float maxAcceleration = 2.0f;
    // How quickly to follow the target
    public float followSpeed = 10.0f;
    // Across which Vector to translate (Default is Y axis)
    public Vector3 translationVector = new Vector3(0, 1, 0);
    // Whether or not to use the scrollwheel acceleration
    public bool scrollWheelAcceleration = true;
	
	void Update () {
        timer+=Time.deltaTime;
        input=Input.GetAxis("Mouse ScrollWheel");

        // This is the acceleration according to the time difference between the "clicks" of the mousewheel
        // If you leave that out, it will be more like Opera scrolling (larger discrete steps but smooth follow)
        // The "300" could be adjusted (lower means larger steps, stronger acceleration)
        if (scrollWheelAcceleration)
        {
            if (input != 0)
            {
                target += Mathf.Clamp((input / (timer * 300)) * speed, maxAcceleration * -1, maxAcceleration);
                timer = 0;
            }
        }
        else // Opera-Style
        {
            target += Mathf.Clamp(input * speed, maxAcceleration * -1, maxAcceleration);
        }
        // As a falloff we use the distance between position and target
        // results in faster Movement at higher distances
        falloff = Mathf.Abs(position-target);

        // Determine the amount of translation for this frame
        translation = Time.deltaTime * falloff * followSpeed;

        // 0.001 is our deadzone
        if (position+0.001 < target)
        {
            this.GetComponent<Transform>().Translate(translationVector*translation*-1);
            position += translation;
        }
        if (position-0.001 > target)
        {
            this.GetComponent<Transform>().Translate(translationVector*translation);
            position -= translation; 
        }
	}
}

```

Play around with it and tell me what you think.

---

<div class="post-metadata">

### Author: ![anon\_77129819](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@anon\_77129819](https://discussions.unity.com/u/anon_77129819)
#### Post date: [December 17, 2009, 3:54pm UTC](https://discussions.unity.com/t/smoothing-mouse-wheel/403866/3 "2009-12-17T15:54:28Z")

</div>

Spot on man, you’re a genious.

Only problem - I have little to no clue what anything does 🙂 especially those math operations. All I know is - it works! Awesomely!

I do have a faint clue of the general idea and the comments do help, but for the most part I can’t explain whats happening. So it’s hard to tweak around with **everything** I’d want…

How do I reverse the direction of the mouse wheel effects? Currently, scroll-fwd goes downward and scroll-bwd goes upward. I’d like that in reverse.

I’ll try to post my JavaScript rendition of this here when I’m done. Can’t thank you enough!

---

<div class="post-metadata">

### Author: ![anon\_742286](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@anon\_742286](https://discussions.unity.com/u/anon_742286)
#### Post date: [December 17, 2009, 8:04pm UTC](https://discussions.unity.com/t/smoothing-mouse-wheel/403866/4 "2009-12-17T20:04:02Z")

</div>

> **[Unity - Scripting API: Mathf.Abs](https://docs.unity3d.com/ScriptReference/Mathf.Abs.html)**

> **[Unity - Scripting API: Mathf.Clamp](https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html)**

Simple Stuff 😉

To invert the direction, just change the Translation Vector to (0,-1,0). I admit it should probably default to the other way around.
