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

Question scrollwheel hold and scroll

Discussion in 'Input System' started by melonhead, May 11, 2023.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    so i currently have the scrollwheel in game as a zoom in and out, is there a way to press down on the scrollwheel button and at the same time then use the scrollwheel to do something different than zoom,like when scroll forward display image for one weapon then when scroll back change image to second weapon, bit stuk with this one guys, help would be much appreciated thank you
     
  2. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    trying this code but cant get it to work, is it not possible to use the scrollwheel when button is down for something different than when button is up?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class mscroll2 : MonoBehaviour {
    6.  
    7.  
    8.     public int roller=0;
    9.  
    10.     public int roller2=0;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.  
    21.          if (!Input.GetKeyDown(KeyCode.Mouse2) && Input.GetAxis("Mouse ScrollWheel") > 0f ) // forward
    22. {
    23.     roller++;
    24. }
    25. else if (!Input.GetKeyDown(KeyCode.Mouse2) && Input.GetAxis("Mouse ScrollWheel") < 0f ) // backwards
    26. {
    27.     roller--;
    28. }
    29.  
    30. roller=roller;
    31.  
    32.  
    33.           if (Input.GetKeyDown(KeyCode.Mouse2) && Input.GetAxis("Mouse ScrollWheel") > 0f ) // forward
    34. {
    35.     roller2++;
    36. }
    37. else if (Input.GetKeyDown(KeyCode.Mouse2) && Input.GetAxis("Mouse ScrollWheel") < 0f ) // backwards
    38. {
    39.     roller2--;
    40. }
    41.  
    42. roller2=roller2;
    43.        
    44.     }
    45. }
     
  3. lyndon_unity

    lyndon_unity

    Unity Technologies

    Joined:
    Nov 2, 2017
    Posts:
    58
    Its likely you want to use Input.GetKey rather than Input.GetKeyDown.
    Input.GetKey will return true while its held, Input.GetKeyDown will only return true on the frame it starts being pressed.
     
  4. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    ok changed code to below, but still when roll mouse wheel with button held down still roller increases but not roller2, i need roller to increase when button up and roller2 to increase when button down and roll scrollwheel, can this not be done

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class mscroll3 : MonoBehaviour {
    6.  
    7.  
    8.     public int roller=0;
    9.  
    10.     public int roller2=0;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.  
    21.          if (!Input.GetKey(KeyCode.Mouse2) && Input.GetAxis("Mouse ScrollWheel") > 0f ) // forward
    22. {
    23.     roller++;
    24. }
    25. else if (!Input.GetKey(KeyCode.Mouse2) && Input.GetAxis("Mouse ScrollWheel") < 0f ) // backwards
    26. {
    27.     roller--;
    28. }
    29.  
    30. roller=roller;
    31.  
    32.  
    33.           if (Input.GetKey(KeyCode.Mouse2) && Input.GetAxis("Mouse ScrollWheel") > 0f ) // forward
    34. {
    35.     roller2++;
    36. }
    37. else if (Input.GetKey(KeyCode.Mouse2) && Input.GetAxis("Mouse ScrollWheel") < 0f ) // backwards
    38. {
    39.     roller2--;
    40. }
    41.  
    42.  
    43.  
    44. roller2=roller2;
    45.        
    46.     }
    47. }
     
  5. lyndon_unity

    lyndon_unity

    Unity Technologies

    Joined:
    Nov 2, 2017
    Posts:
    58
    That code works for me using the middle mouse button.

    I would suggest refactoring to read Input.GetKey(KeyCode.Mouse2) into a temporary variable (likewise with Input.GetAxis("Mouse ScrollWheel")) so you make less duplicate function calls.


    If this is specific to a certain kind of mouse then you could raise a bug report with the specific hardware details.

    You could also try the input debugger in window, analysis section of the menu. Select / double click the mouse entry and the click back to the game window to direct input to the game. Clicking the middle button triggers the 'middleButton' state to change.


    You could also experiment with the input system package which is more powerful and might be of interest:
    https://docs.unity3d.com/Packages/com.unity.inputsystem@1.5/manual/Workflow-Direct.html
    https://docs.unity3d.com/Packages/com.unity.inputsystem@1.5/api/UnityEngine.InputSystem.Mouse.html
     
  6. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    thanks for the heads up about the mouse, i tried another one and it works fine, cant understand that, mouse brank looks like enospeak or similar on the sticker it is very small but wont work for this script for some reason