Search Unity

LMB down while RMB hold

Discussion in 'Scripting' started by IntelligentDesign, Jun 13, 2016.

  1. IntelligentDesign

    IntelligentDesign

    Joined:
    Jun 13, 2014
    Posts:
    51
    I have a boolean I want to set to true while the right mouse button is held down. However, upon either letting go of the RMB or pressing the LMB I want to set the boolean back to false. I've tried two ways of doing this:

    Code (CSharp):
    1. if (Input.GetMouseButton(1)) {
    2.   bool = true;
    3.   if(Input.GetMouseButtonDown(0)) {
    4.     bool = false;
    5.   }
    6. }
    and

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(1)) {
    2.   bool = true;
    3. } else if (Input.GetMouseButtonUp(1) || Input.GetMouseButtonDown(0)) {
    4.   bool = false;
    5. }
    and yet neither one recognizes the LMB being pressed while the RMB is held down. Does anyone know a solution to this?
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Is that in Update()? The ***Down() is true only when it's clicked. So in your first code bool should go false but just for 1 frame. On next one forward it goes back to true. Solution: change GetMouseButtonDown to GetMouseButton.

    Second code should work though? Might be something else interfering with your code?

    And as always, use Debug.Log() to print info for clicks when they happen.
     
  3. IntelligentDesign

    IntelligentDesign

    Joined:
    Jun 13, 2014
    Posts:
    51
    I am, but the logs don't even appear for the left mouse button inputs if I'm still holding down the right mouse button. It's almost as if the RMB input prevents a LMB input from being read
     
  4. IntelligentDesign

    IntelligentDesign

    Joined:
    Jun 13, 2014
    Posts:
    51
    I've realized that this issue may be a Windows thing and not a Unity thing. Turns out that the Windows mouse driver is designed to only recognize a right click on the button's release. So as a result it would seem that Unity can't detect any other mouse input until the right mouse button is released (unlike Macs or Linux systems where the right mouse "click" is detected on the down press.) Unfortunately though I have no idea what the workaround here would be :(

    For the record I tried my code using the I and O keys in place of the LMB and RMB and it worked fine
     
  5. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    This works for me.
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Mouse1))
    2. {
    3.     b = true;
    4. }
    5. else if (Input.GetKeyUp(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.Mouse0))
    6. {
    7.     b = false;
    8. }
     
    Last edited: Jun 14, 2016
  6. IntelligentDesign

    IntelligentDesign

    Joined:
    Jun 13, 2014
    Posts:
    51
    Still having trouble getting the LMB to be recognized if there's any other inputs taking place. Like if I click it without pressing any other keys it works, but otherwise it doesn't

    EDIT: it works if I use my actual mouse! However, if I try to use the trackpad mouse on my Asus laptop it has issues. Anybody have any clue why?
     
    Last edited: Jun 14, 2016