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

GetMouseButton incorrect after a few seconds

Discussion in 'Scripting' started by RedHotFlashman, Aug 15, 2019.

  1. RedHotFlashman

    RedHotFlashman

    Joined:
    Mar 16, 2018
    Posts:
    31
    Hello,
    I simply want to know if the mouse is held down, so I can do mouseMove actions when the mouse is held down and dragged. But I noticed that the GetMouseButtonUp was not always triggered, so I added !GetMouseButton to perform an extra check. But when I hold the mouse down for more then 3 seconds, GetMouseButton returns false, even though the mouse is pressed down.

    Stripped version of the code,
    Tested on unity 2018 and 2019:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class TestMouseDown : MonoBehaviour
    7. {
    8.     bool mouseDown;
    9.  
    10.     private void Update()
    11.     {
    12.         // Mouse Click
    13.         if (Input.GetMouseButtonDown( 0 ))
    14.         {
    15.             mouseDown = true;
    16.         }
    17.  
    18.         // Mouse Up
    19.         if (Input.GetMouseButtonUp( 0 ))
    20.         {
    21.             mouseDown = false;
    22.         }
    23.         if (!Input.GetMouseButton( 0 ))
    24.         {
    25.             mouseDown = false;
    26.         }
    27.  
    28.         Debug.Log( "mouseDown: "+mouseDown );
    29.     }
    30. }
    31.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    That's bizarre. I've just tested using this:
    Code (csharp):
    1.  
    2.     public bool tester;
    3.     private void Update()
    4.     {
    5.         tester = Input.GetMouseButton(0);
    6. }
    And it behaves as expected, with the "tester" bool remaining true after 3 seconds of holding down the button.

    Is it possible this is something specific to your hardware? Could you try a different mouse and/or different computer? What OS are you using?
     
  3. RedHotFlashman

    RedHotFlashman

    Joined:
    Mar 16, 2018
    Posts:
    31
    I am using a 2017 iMac osx 10.13.6, with Logitech mouse. I don't have another mouse to test.
    I used several online mouse check pages like this one (JavaScript based I assume), and no problem there.
     
  4. RedHotFlashman

    RedHotFlashman

    Joined:
    Mar 16, 2018
    Posts:
    31
    Problem solved:
    I installed the Logitech Unifying Device software and re-paired my mouse to the computer, that solved my problem.
     
    Joe-Censored likes this.
  5. yqiazkv

    yqiazkv

    Joined:
    Nov 11, 2022
    Posts:
    2
    My computer runs Mac OS X 2019, and I use a Razer mouse. It's not possible for me to test another mouse. There was no problem using several online mouse check pages like this one (JavaScript-based, I assume).
     
    Last edited: Jan 22, 2023