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

Confused on how Input.GetAxis works

Discussion in '2D' started by manugarciac, Aug 7, 2016.

  1. manugarciac

    manugarciac

    Joined:
    Aug 7, 2016
    Posts:
    3
    I'm trying this code:
    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.         float h = Input.GetAxis ("Horizontal");
    4.         Debug.Log (h);
    5.     }
    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?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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.
     
  3. IShol

    IShol

    Joined:
    Jun 26, 2015
    Posts:
    17
    use void Update
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         print(Input.GetAxis("Horizontal") + "     " + Input.GetAxis("Vertical"));
    4.     }
     
    LiterallyJeff likes this.
  4. manugarciac

    manugarciac

    Joined:
    Aug 7, 2016
    Posts:
    3
    Didn't work. Same result. I'm confused.
     
  5. manugarciac

    manugarciac

    Joined:
    Aug 7, 2016
    Posts:
    3
    Maybe there is something wrong with my keyboard... I experience the same issue while playing the Tower Defense demo.
     
  6. IShol

    IShol

    Joined:
    Jun 26, 2015
    Posts:
    17
    true