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. Dismiss Notice

Mouse WheelScroll

Discussion in 'Scripting' started by White-Gun, Jun 26, 2016.

  1. White-Gun

    White-Gun

    Joined:
    Mar 11, 2016
    Posts:
    5
    Can someone explain how Mouse WheelScroll axis work as i have tried multiple different scripts & solutions i found on the internet but nothing works all i need is to make an object rotate on Y axis to right when wheel is scrolled up and left if wheel is scrolled down

    Once i can figure it out i will implement it in my game in which i can already pick up objects and able to hold object with Q (temp use input) but i want to be able to spin object model using Mouse WheelScroll up/down to rotate object so i can get a view of the objects model from all sides.

    Rotation explanation:
    Wheel up = Rot right
    Wheel down = Rot left




    all advice will be appreciated!
    Thanks in advance!
     
  2. White-Gun

    White-Gun

    Joined:
    Mar 11, 2016
    Posts:
    5
    ? anyone ?
     
  3. Socrates

    Socrates

    Joined:
    Mar 29, 2011
    Posts:
    787
    You can get the mouse scroll wheel information using standard Unity inputs. By using Input.GetAxis("MouseScrollWheel"), you can determine how much the mouse wheel was turned.

    Here's is some simple code I used to make a basic cube rotate around the Y axis. You need to drag and drop the object you want to rotate into the "whatToRotate" field.

    Code (CSharp):
    1.     public class MouseWheelRotation : MonoBehaviour
    2.     {
    3.         // The object you want to rotate.  I used a simple cube.
    4.         public GameObject whatToRotate;
    5.  
    6.         // A speed modifier to tune how fast the object rotates as the mouse wheel turns.
    7.         public float rotationSpeed = 100.0f;
    8.  
    9.  
    10.         void Start ()
    11.         {
    12.             if (whatToRotate == null)
    13.             {
    14.                 Debug.Log("WARNING:  No object to rotate.");
    15.             }
    16.         }
    17.  
    18.      
    19.         void Update ()
    20.         {
    21.             // Get the amount the mouse wheel moved from the standard Unity Input.
    22.             float _rotation = Input.GetAxis("Mouse ScrollWheel");
    23.  
    24.             // Rotate the object based on the given input.
    25.             //   Vector3.up is because you want to rotate around the Y axis.
    26.             //   _rotation is how much input was recieved.
    27.             //   rotationSpeed is a modifier speed up the rotation.
    28.             //   -1.0f is needed for mousewheel up to rotate to the right.
    29.             //
    30.             //   Space.World is so that the object rotates in world space around the Y axis.
    31.             //   Leave that out if you want the object to rotate around it's personal Y axis.
    32.             whatToRotate.transform.Rotate(Vector3.up * _rotation * rotationSpeed * -1.0f, Space.World);
    33.         }
    34.     }
    35.  
    https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
     
  4. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    I tried this, and it seems to work:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseScroll : MonoBehaviour {
    5.  
    6.     public float scrollSpeed = 100f;
    7.  
    8.     // Update is called once per frame
    9.     void Update () {
    10.         float scrollAmount = Input.GetAxis ("Mouse ScrollWheel");
    11.         this.transform.Rotate (Vector3.up, -scrollAmount * scrollSpeed);
    12.  
    13.     }
    14. }