Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Legacy Input System - Differentiate Between Mouse & Touch

Discussion in 'Input System' started by bacon_nugget, Jan 18, 2023.

  1. bacon_nugget

    bacon_nugget

    Joined:
    Nov 8, 2021
    Posts:
    2
    I'm using the old input system and I'd like to be able to check whether a mouse or a touch screen was used. Some devices, like a laptop with a touch screen, can have support for both.

    This is my current strategy:

    Code (csharp):
    1. // touch check
    2. var touchUsed = Input.touchCount > 0;
    3.  
    4. // mouse check
    5. var pos = Input.mousePosition;
    6. var mouseUsed = !pos.Equals(_prevMousePos);
    7. _prevMousePos = pos;
    The problem is that Input.mousePosition is updated by both touches and mouse movement. When the user touches the screen, touchUsed and mouseUsed are true. I thought disabling Input.simulateMouseWithTouches might fix it, but it did not seem to change anything. This StackOverflow post suggests that it might have something to do with execution order. This forum post says that it only covers clicks/taps, not motion. I'm not sure what else to try.

    Thanks for your time!

    Editor version is 2021.3.16f1
     
    Last edited: Jan 18, 2023
  2. bacon_nugget

    bacon_nugget

    Joined:
    Nov 8, 2021
    Posts:
    2
    I realized that I could also check for touches in the mouse control script and return false if a touch is present. This won't capture situations where a mouse and touch screen are used simultaneously, but it's better than before.

    Code (CSharp):
    1. var mouseUsed = !pos.Equals(_prevMousePos) && Input.touchCount == 0;
    If anyone learns something more about what's going on with Input.simulateMouseWithTouches, please reply below!