Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Multiplayer Play Mode virtual players do not get input from keyboard

Discussion in 'Multiplayer Tools' started by Yoraiz0r, Aug 4, 2023.

  1. Yoraiz0r

    Yoraiz0r

    Joined:
    Apr 26, 2015
    Posts:
    78
    Using Unity 2023.1.3f1, MPPM 0.2.0, and built in unity input system (not the 'new' 'input system', but the Input class), I am witnessing that virtual players completely ignore input values coming from my keyboard.

    It started innocently enough when GetAxisRaw("Horizontal") was failing on the virtual player. But then I checked GetAxis("Fire1") which takes input from both left mouse and ctrl key on keyboard by default, and the results were interesting.

    The virtual player was accepting input from the mouse, but not from the control key on my keyboard. Same code. The editor player did accept input from both devices just fine.

    What's going on here? is there some code hacking the keyboard as reserved for the editor player? any way to make keyboard inputs from legacy input system work?

    Edit: I also tried Rewired and can confirm that it is broken for virtual players as well

    Edit2: I also tried the new Input System and can report that this one works, but with a caveat! you have to select the virtual player, deselect it, and select it again before keyboard input starts being respected... frustrating.

    Edit3: It got permanently broken for new input system as well until I restarted the unity editor. I noticed this happen immediately after a domain reload. Am wondering if there is a connection.
     
    Last edited: Aug 4, 2023
    saskenergy likes this.
  2. Yoraiz0r

    Yoraiz0r

    Joined:
    Apr 26, 2015
    Posts:
    78
    After messing with this for a while and pulling the input system package source code and starting to mess with it in hopes of understanding and fixing the root cause, I have a few more details to share on the issue:

    1. built in Input & Input from Rewired both fail to detect anything from the keyboard at all.
    2. the new Input System package fails to get input - until you unfocus and refocus the editor / its game view. Clicking into the virtual player after entering playmode will result in no input, if you then click out of the virtual player's gameview (either the console, or a different application) and then click back on the virtual player's game view, keyboard input will be respected.
    3. The above does not work with the built in Input.
    4. Given the nature of this being somewhat focus based, I checked Input System's package's InputManager class. No combination of settings in the input system assets help. I tried setting background behavior to ignore focus, editor playmode input behavior to all device input always goes to game view, and I even called input system's 1.6.2's
    InputSystem.runInBackground = true;
    to no avail.
    5. I added a debug.log to the InputManager's OnFocusChanged callback, and there is one discrepency I do see. Specifically in that the main editor from which you press play, calls OnFocusChanged(true) twice, from these locations
    Code (CSharp):
    1. UnityEditor.EditorApplicationLayout:InitPlaymodeLayout ()
    2. UnityEngine.Application:InvokeFocusChanged (bool)
    3.  
    The virtual player only sets focus from the second one. I checked the cs reference but can't tell if there is anything significant there or in the related PlayModeView class.
    7. I tried adding an editor script that on entering playmode waits for a bit before setting focus to game view, console, game view. While this calls the InputManager's OnFocusChanged successfully, the keyboard still gets no input unless I manually perform the steps mentioned in point #2.
    8. I tried manually calling OnFocusChanged(false) once on start for the virtual player... and it worked!

    My current understanding is that entering playmode for the virtual player sets the window to act as focused. The input enters some weird state in which it won't poll for anything. If I properly inform the virtual players that they're not focused, then I end up with functional input as I switch over to them.

    I hope this this can be addressed and helps making future versions of MPPM better!

    Is there anything that is wrong in this assumption? if there's something that's better / smarter to do I'd love to know about it please.

    Here's the code I'm currently relying on due to lack of a method to unfocus a window in unity.
    Code (CSharp):
    1.  
    2. #if UNITY_EDITOR
    3.         private static void StealFocusFromGameViewAndGiveItToConsole()
    4.         {
    5.             var consoleType = Type.GetType("UnityEditor.ConsoleWindow,UnityEditor");
    6.              EditorWindow.GetWindow(consoleType, false, null, focus: true);
    7.         }
    8. #endif
     
    Last edited: Aug 5, 2023
  3. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    416
    Thanks for the detailed report! May I know what OS you're using?
     
    Yoraiz0r likes this.
  4. Yoraiz0r

    Yoraiz0r

    Joined:
    Apr 26, 2015
    Posts:
    78
    Windows 10!

    If there's any solution/quick hack that allows me to fix this editor focus issue without the new input system I'd love to know, if available, please.

    I confirmed the quick hack above only works for the new input system.
    I also confirmed that the 0.3.0 update of MPPM does not result in any fix for this, and am sticking to 0.2.0 right now.
     
  5. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    416
    I tried on a Windows 10 machine and can't quite reproduce the behavior you're observing. When I start a clone keyboard input indeed is not working, but that's because the focus is on the console. If I just click inside the game view then keyboard input works as expected. It's an annoyance for sure, and something we'll look into fixing, but it doesn't look like the issue you're reporting here.

    Are you in a position to try this on a more recent version of Unity? I tried on 2023.2.0a22. Could be that there was a fix made in the editor related to this. If that's the case, I could track down the fix and ensure it gets backported to 2023.1.
     
    Yoraiz0r likes this.
  6. Yoraiz0r

    Yoraiz0r

    Joined:
    Apr 26, 2015
    Posts:
    78
    I'm only seeing 2023.2.0b3 on unity hub, would that suffice for this reproduction test?

    And indeed, I do mean something more specific than just 1 extra click - I could not get any input despite any attempt to move focus between console and game view on the version I tried.
     
  7. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    416
    Yes, 2023.2.0b3 should be fine. It's even more recent than the version I tried to reproduce this with.
     
    Yoraiz0r likes this.
  8. Yoraiz0r

    Yoraiz0r

    Joined:
    Apr 26, 2015
    Posts:
    78
    Okay, I've installed 2023.2.0b3 (beautiful launch screen and awesome search bar btw), MPPM 0.3.0, a minimal test script that would have replicated the issue, and....
    I'm happy to report that the bug is not there, the 'first focus' issue is not there either! This is great!
    The virtual players also seem to keep a consistent view when out of playmode which is fantastic!
    And if I change the scene and save they immediately reflect the changes in their gameview, woo!
    This is so much better than my experience with 2023.1 and MPPM 0.2.0, incredible!
     
    simon-lemay-unity and saskenergy like this.
  9. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    416
    That's great! Thanks for reporting back! I'm glad to read the experience with 2023.2 is better. I'll dig out what changes could have fixed your issue in-between 2023.1 and 2023.2 and request that they be backported.
     
    Yoraiz0r likes this.