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

Question Possible issue related to Input.GetAxis("Horizontal") and Input.GetAxisRaw("Horizontal")!?

Discussion in 'Scripting' started by d0o0b, Dec 21, 2022.

  1. d0o0b

    d0o0b

    Joined:
    Jan 12, 2016
    Posts:
    2
    Hi.

    I'm just wondering about the following simple code and if Input.GetAxisRaw("Horizontal") should default to 0. I keep getting -1 by default. Is there an issue with the code or Input.GetAxisRaw()? I get the same results with Input.GetAxis()


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Proto2PlayerController : MonoBehaviour
    4. {
    5.     public float horizontalInput;
    6.    
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         horizontalInput = Input.GetAxisRaw("Horizontal"); // Defaults to -1   BUG?
    11.     }
    12. }
    I'm currently using Unity 2021.3.16.f

    Thanks for any feedback :0)
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Often this comes from you having left an input device plugged into your computer and forgetting about it. For example a gamepad lying upside down leaning on its joystick.
     
  3. d0o0b

    d0o0b

    Joined:
    Jan 12, 2016
    Posts:
    2
    Thanks.
    I tried to disconnect all devices connected to the computer, but the issue was still there.

    Ended up with this workaround.


    Code (CSharp):
    1. using UnityEngine;
    2. public class Proto2PlayerController : MonoBehaviour
    3. {
    4.     public float horizontalInput;
    5.  
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.         horizontalInput = 0f;
    10.         if (Input.GetButton("Horizontal"))
    11.         {
    12.             horizontalInput = Input.GetAxisRaw("Horizontal"); // Defaults to -1   BUG?
    13.         }
    14.     }
    15. }
    16.