Search Unity

Input.GetAxis

Discussion in 'Getting Started' started by filiphdan, Aug 22, 2017.

  1. filiphdan

    filiphdan

    Joined:
    Aug 15, 2017
    Posts:
    14
    Hello,

    Im just getting started with Unity, im now doing my 3rd tutorial but im having this doubt, wich i think its a beginner doubt wich is : wen i use the Input.GetAxis to move the player object i use a variable to store the value like float moveHorizontal = Input.GetAxis ("Horizontal") i just wanted to know the ("Horizontal") refers to the Horizontal Input.GetAxis? i ask because to me it looks like a String value.
    So for what i understand wen i write ("Horizontal") or ("Vertical") unity recognises this as the x, y right?

    Simple doubt but just wanted to clarify so i can continue understanding all the code.

    Thanks!
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Welcome to the forums!

    Input.GetAxis is a method that accepts a single parameter, which as you identified, is a string. I always thought it was silly to use a string instead of an enum, but I realized it's because you can, through Unity's Input system, define your own axes as well, which you would access by passing the name of the axis into GetAxis.

    Anyway, although you pass a string into GetAxis, it returns a float value out. This float is a value between -1 and 1 representing the normalized position of the input on that axis.

    So while by default passing "Horizontal" or "Vertical" into the method is interpreted as the x and y values of your primary input, you could, if you liked, modify that definition.
     
    filiphdan and Bill_Martini like this.
  3. filiphdan

    filiphdan

    Joined:
    Aug 15, 2017
    Posts:
    14
    Great explanation Schneider, rely helpful.
    Cheers!
     
    JoeStrout likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can configure all the strings you use in the Input Manager. Open that up and have a look, once you've seen it, GetAxis will make a whole lot more sense.