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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Controller Inputs Changing Unexpectedly First Time Run

Discussion in 'Input System' started by wallace9958, May 2, 2022.

  1. wallace9958

    wallace9958

    Joined:
    Nov 1, 2021
    Posts:
    3
    Hey there,

    Think I've got a bug here. Anybody else seen this?

    For reference, I am using:
    1: An Xbox Elite Series Two Controller.
    2. Unity 2021.3.0f1

    I've broken it down to a simple script to isolate behavior.

    Problem Description:

    When I leave the Unity Editor for another window (e.g., Visual Studio Code) and then return, the first time I press the play button and enter into the game world (either maximized / focused or unfocused and clicking in), my inputs joystick axis inputs change unexpectedly.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class InputTest : MonoBehaviour
    4. {
    5.     void Update()
    6.     {
    7.         PanCamera();
    8.     }
    9.  
    10.     private void PanCamera()
    11.     {
    12.         {
    13.             float horizontalInput = Input.GetAxis("Horizontal");
    14.             float verticalInput = Input.GetAxis("Vertical");
    15.             print("Horz" + horizontalInput);
    16.             print("Vert" + verticalInput);
    17.         }
    18.     }
    19. }
    Script is on an empty, I am using unfocused to debug, and I never touch the controller during this process.

    Repro steps:
    1. I press play and the scripts yields 0 for horizontal and 0 for vertical as expected.

    Start Unfocused.png

    2. I left mouse-click into the window and the script yields -1 for horizontal and 1 for vertical unexpectedly.

    Click In.png

    3. I click play to stop.
    4. I click play to start.
    5. I left mouse-click into the window and the script yields 0 for horizontal and 0 for vertical as expected and performs normally throughout play.
     
  2. dmytro_at_unity

    dmytro_at_unity

    Unity Technologies

    Joined:
    Feb 12, 2021
    Posts:
    212
    On which platform this is?
     
  3. wallace9958

    wallace9958

    Joined:
    Nov 1, 2021
    Posts:
    3
    Desktop on Win11.

    Just for reference, I have another older project running on 2019.4.37f1 and it doesn't see this behavior.

    I updated to latest 2021.3.1f1 and it still exists.

    For clarification on behavior, it sets those values and holds them; they don't revert at any point. So, essentially, I end up with a camera that pans in 2d flying off northwest / top left.
     
  4. wallace9958

    wallace9958

    Joined:
    Nov 1, 2021
    Posts:
    3
    I went ahead and did some further testing on this.
    • The controller is an unrelated component.
    • I first saw the behavior in 2021.3.1f1 and then also checked for and found it in 2020.3.33f1.
    • Retested with current LTS versions and the issue still appears in 2021.3.2.f1 and 2020.3.34f1.
    • It does not appear on 2019.4.38f1.
    • I have not checked any other versions.
    • I am playing in Unfocused Play mode.
    • I am using default Axes without any modification.
    Reproduction steps are the same. I tested with a simple script attached to the default camera of brand new URP pipeline projects:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GetInputAndPrint : MonoBehaviour
    4. {
    5.     float x;
    6.     float y;
    7.  
    8.     void Start()
    9.     {
    10.         x = Input.GetAxis("Horizontal");
    11.         y = Input.GetAxis("Vertical");
    12.         print("initial x " + x);//comment
    13.         print("initial y " + y);
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (Input.GetAxis("Horizontal") != x) {
    19.             x = Input.GetAxis("Horizontal");
    20.             print("updated x " + x);
    21.         }
    22.                 if (Input.GetAxis("Vertical") != y) {
    23.             y = Input.GetAxis("Vertical");
    24.             print("updated y " + y);
    25.         }
    26.     }
    27. }
    28.  
    It essentially just prints the initial state as well as any updates to the axis.

    Here are some behavioral observations:
    When I edit and save a script and click back into the editor while the Game view is the active tab, it incorrectly initializes the inputs to x=-1, y=1 the first time I enter the game view.

    If my first click into the Unity editor is a left mouse-click in a tab other than the game tab while playing unfocused (e.g., the console), it then correctly updates to x=0,y=0. My first click into the Game view sets it to x=-1, y=1.

    It does the same when I just click directly into the game tab; x=-1, y=1.

    If I connect a controller while in this state, I am only ever to get update to change y to -1 by pressing down. It then returns to 1 upon release. X never changes regardless of where I tilt the stick.

    When I hit play again to stop the game, update returns x=0, y=0 correctly before stopping. Then, when I play again, it correctly starts at x=0, y=0 and proceeds to behave normally until I update scripts again.

    I can also correctly reset the inputs by clicking into another tab (e.g., console) and then back into the game tab again. This is my current workaround since I don't have to play and stop repetitively.