Confused on how Input.GetAxis works

I’m trying this code:

    void FixedUpdate ()
    {
        float h = Input.GetAxis ("Horizontal");
        Debug.Log (h);
    }

If I run that and keep presing the left key, it goes all the way to -1. However, if I press any other key without releasing the left key, it goes back to 0. I’m trying to make my player keep moving after jumping, without having to press the arrow key again. How can I do this?

If you’re only going to be using Keyboard controls, then you don’t need to use Axis. Axis is great for cross platform and joystick controls. But you can check if a specific keyboard key is pressed/released/held at any time using Input.GetKeyDown, GetKeyUp, and GetKey respectively, using the KeyCode enum for a parameter ie. (KeyCode.LeftArrow).

I don’t think you would have that problem on a controller using GetAxis. It may have to do with your keyboard’s ability to send simultaneous keystrokes. Not really sure as I haven’t encountered that issue.

use void Update

void Update ()
    {
        print(Input.GetAxis("Horizontal") + "     " + Input.GetAxis("Vertical"));
    }
1 Like

Didn’t work. Same result. I’m confused.

Maybe there is something wrong with my keyboard… I experience the same issue while playing the Tower Defense demo.

true