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. Dismiss Notice

Question Input.GetAxis never outputs 0 (keyboard as input device)

Discussion in 'Scripting' started by Alban_T, Jul 5, 2023.

  1. Alban_T

    Alban_T

    Joined:
    Jul 5, 2023
    Posts:
    2
    HI,

    We are trying to debug an issue my son has on my laptop with his Unity game.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Keyboard : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         Debug.Log(Input.GetAxis("Horizontal") + "\t" + Input.GetAxis("Vertical"));
    17.     }
    18. }
    I would expect the debug to show "0 0" when I'm not pressing any arrow or WSAD keys on my keyboard.
    But in reality it outputs "-1 1" and when I press the arrow keys they switch to -1/1 but never to 0.

    I found several questions about this but as far as I could see those questions involved a joystick or gamecontroller which was not correctly calibrated. But I'm having these issues with my laptop keyboard.

    Does anyone have a suggestion on what could be causing this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Check the Input settings (Edit -> ProjectSettings -> Input)

    By default there are other things mapped to these axes.

    Normally those things don't cause problems.

    Perhaps in your case they do.

    You could try isolating them so there is only key input.

    ALWAYS use source control so you can instantly revert any potential changes you make anywhere with just one click.

    Also, I find it better to check the keys I want rather than read the axes, something like Line 115 here:

    https://github.com/kurtdekker/proxi...emoCoyoteTimeJumping/DemoCoyoteTimeJumping.cs

    or Line 182 here:

    https://github.com/kurtdekker/proxi...TwinStickShooter/TwinStickPlayerController.cs

    Another option would be the new input system, which replaces the old input system above entirely.
     
    Alban_T likes this.
  3. Alban_T

    Alban_T

    Joined:
    Jul 5, 2023
    Posts:
    2
    Thanks!!!

    I think I fixed it by changing the dead value from 0.0001 to 0.1
    upload_2023-7-5_22-58-43.png

    I would have programmed it using the keycodes like you showed in your examples.
    I'll try to educate him :p He is 13 and this is his first real experience with C# after a little Python.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    That's so awesome. Hope you two have LOADS of fun with this stuff... I wish I had Unity when I was 13!!