Search Unity

Axis input gets stuck at "left" after alt-tab

Discussion in 'Scripting' started by eobet, May 29, 2017.

  1. eobet

    eobet

    Joined:
    May 2, 2014
    Posts:
    176
    Even if no input is given before I switch to another application, my character spins left forever, regardless of new input when I switch back to Unity with Play mode running.

    Anyone know why?

    Relevant code in Update:

    Code (CSharp):
    1.             float forwardMotion = Input.GetAxis("Vertical");
    2.             float sideMotion = Input.GetAxis("Horizontal");
    3.  
    4.             if (sideMotion != 0) {
    5.  
    6.                 sideMotion = (sideMotion > 0 ? 1 : -1);
    7.                
    8.                 isMoving = true;
    9.                 moveStart = Time.time;
    10.                 moveTime = turnTime;
    11.                
    12.                 playerDirection += (int)sideMotion;
    13.                
    14.                 if (playerDirection < 0) {
    15.                     playerDirection = 3;
    16.                 }
    17.                 if (playerDirection > 3) {
    18.                     playerDirection = 0;
    19.                 }
    20.                
    21.                 ChangeDirection ();
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    By alt-tab, I assume you're referring to the Windows standalone platform.

    A common problem in Input programming is that when you hold down a key, then alt-tab, the key-up event is lost. So your app is stuck thinking that key is still down. The application needs to be in focus in order to receive these input events. To get out of this, you'd need to hit that key and release it while the app is in focus, which triggers the key up event.

    To better deal with this in code, one workaround is to use Input.GetKeyDown instead of Input.GetAxis if you can. Never ever rely on needing both KeyDown and KeyUp events.
     
  3. eobet

    eobet

    Joined:
    May 2, 2014
    Posts:
    176
    Actually, putting alt-tab in the title was misleading.

    If I just switch applications with the mouse it works the same.

    And I want to use getAxis instead of KeyDown because I want to support gamepads in the future.

    To add to this, when I press an arrow key, the getAxis float is something like -0.04 or 0.05 when I detect it in the code above, but when switching back to the Unity Editor in Play mode, it's stuck full tilt at -1.
     
    Last edited: May 29, 2017
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  5. NickWWCC

    NickWWCC

    Joined:
    May 30, 2017
    Posts:
    1
    I'm also experiencing this issue but only on Windows 10. My Mac does not have the issue. With the simplest example possible (NetworkManager and a PlayerCube) between applications, the app that becomes unfocused gets its Horizontal Axis stuck at -1 and it's Vertical Axis stuck at 1. I was not holding any movements keys when I changed windows, so it's not a KeyUp event being lost. If I refocus the window and try to move the player cube, the Input remains frozen with some sporadic movement. Unity 5.6.1f1, 64bit, Windows 10

    Update
    The problem seems to be coming from virtual joystick input. If you look under your Input Settings, you'll notice you have two entries for Horizontal and Vertical. One is for joysticks, and the other is for keyboards. I just removed my Joystick entries and the problem was resolved. Found this forum post to be helpful in fixing my issue. http://answers.unity3d.com/questions/303708/inputgetaxishorizontal-problem.html
     
    Last edited: May 31, 2017
    eobet likes this.
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Interesting update. I will try to remember that if I come across a post about this subject in the future. :)
     
  7. eobet

    eobet

    Joined:
    May 2, 2014
    Posts:
    176
    Thanks, that was it!

    And apparently, this is a five year old bug. The second several year old bug in Unity I've encountered in a very short time using it. Not good. :(
     
  8. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    862
    I had the same issue in Unity 2017.1.1. The joystick on my gamepad was stiking to -1 and 1. Chaning Joy Num from "Get Motion From All Joysticks" to "Joystick 1" in InputManager fixed the problem for me. Perhaps Windows thought one of my other devices was a joystick, because Windows.