Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Understanding MouseScroll Wheel Input

Discussion in 'Scripting' started by Cyrussphere, Jul 1, 2016.

  1. Cyrussphere

    Cyrussphere

    Joined:
    Jul 1, 2016
    Posts:
    129
    Hey all,

    I am brand new to unity and been running through some tutorials while on the side putting together a script for movement of spaceship. So far I have been able to get most of this down in C# but I cannot seem to wrap my head around Input.GetAxis("MouseScrollWheel") and exactly how it functions.

    My end goal is to have mouse scroll wheel up adds to speed by 1 until it caps at 10 and then mouse wheel down drops speed back down by 1 until it returns to 0. I guess I am having a hard time understanding the mouse wheel without having a defined positive and negative buttons.

    Any help in understanding this input function would be great!
     
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    it returns the sensitivity value in input settings when you scroll, negative value when scrolling down, positive when up, at least for me

    so to change a value by increments of 1 you could for example set the sensitivity to 1 and use

    Code (CSharp):
    1.  
    2.     int n = 0;
    3.     void Update()
    4.     {
    5.         n += Mathf.RoundToInt(Input.GetAxis("Mouse ScrollWheel"));
    6.         n = Mathf.Clamp(n, 0, 10);//prevents value from exceeding specified range
    7.         Debug.Log(n);
    8.     }
    you could also use float instead of int which is what GetAxis returns but if you need just integers might as well convert it to int to avoid float inaccuracy in case you want to use the value for stuff where that becomes a problem. for doing that Mathf.RoundToInt function is just one way out of many, it works here since it rounds to nearest integer. theres also GetAxisRaw but not sure how that works

    version without converting to int:

    Code (CSharp):
    1.  
    2.     float n = 0;
    3.     void Update()
    4.     {
    5.         n += Input.GetAxis("Mouse ScrollWheel");
    6.         n = Mathf.Clamp(n, 0, 10);//prevents value from exceeding specified range
    7.         Debug.Log(n);
    8.     }
     
    Last edited: Jul 1, 2016
    Cyrussphere likes this.
  3. Cyrussphere

    Cyrussphere

    Joined:
    Jul 1, 2016
    Posts:
    129
    Awesome, thanks for the reply. That definitely helps me in understanding it and will give me something to start with when i'm off work. Thanks!