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

Meaning of virtual axis

Discussion in 'Scripting' started by jmaldonado93, Feb 5, 2014.

  1. jmaldonado93

    jmaldonado93

    Joined:
    Oct 19, 2013
    Posts:
    89
    I am starting developing with Unity3D. I have been searching for an explanation of what a virtual axis means. This term is used in, form example, Input.GetAxis. Can someone please explain or let me know where can I find information about this issue?

    Thanks in advance.
     
    captain_oats likes this.
  2. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
  3. jmaldonado93

    jmaldonado93

    Joined:
    Oct 19, 2013
    Posts:
    89
    I appreciate your response.
    Yes, the link gives more information of what I actually had, but there is still something that I do not understand.
    (By the way, I am totally new to Unity3D, this is why I am having trouble with some concepts.)
    Documentation says that an axis has a value between -1 and 1, and that zero is neutral position.
    Does this mean that, for example, an axis can take a value of 0.3 or -0.45?
    What is the correct interpretation if Input.GetAxis("Horizontal") or Input.GetAxis("Vertical") or Input.GetAxis("Jump") has a positive or negative value?
    Are "Horizontal", "Vertical" and "Jump" all virtual axis?

    Respectfully,
    Jorge Maldonado
     
  4. Gale

    Gale

    Joined:
    Jun 22, 2012
    Posts:
    40
    The Axis takes input and puts it into an easy to use value for you to use.

    The default keys for Vertical Axis are "Up Arrow" "Down Arrow" "W" and "S".
    Default Keys for Horizontal are "Left Arrow" "Right Arrow" "A" and "D".

    Both also recognize controller and joystick input.

    Using the Input.GetAxis will slowly increment the Axis towards 1 or -1 depending on the direction of the Input.
    So if you push the Up Arrow the Input.GetAxis would start towards 1. A couple of frames of this may look like this.

    Frame 1: Horizontal Axis = 0.4
    Frame 2: Horizontal Axis = 0.8
    Frame 3 : Horizontal Axis = 1
    Frame 4 : Horizontal Axis = 1
    ...

    If you then stop pressing the Up key or alternately press the Down Key it will begin going to 0 (and then -1 if pressing down) in a similar fashion.

    They count from 1 to -1 and have 0 be neutral because then it is easy to do something like.

    function Update ()
    {
    var movement = Input.GetAxis("Horizontal") * 20 ;
    transform.Translate(movement, 0, 0);
    }

    when pressing left movement will come out to be -20
    when pressing nothing it will come out to 0
    and pressing right will give you 20

    With just that small bit of code you can move an object left to right (left being -1, right being 1) 20 units each frame.


    If you don't want to have those few frames where the Axis is 0.33 or something like that then you can use GetAxisRaw which is essentially the same thing but will always return a whole number (-1, 0, or 1).
     
  5. jmaldonado93

    jmaldonado93

    Joined:
    Oct 19, 2013
    Posts:
    89
    THANK YOU SO MUCH GALE.
    Your explanation is great. My question is now completely answered.
     
  6. captain_oats

    captain_oats

    Joined:
    Sep 12, 2014
    Posts:
    1
    Thank you GALE! The explanation was very helpful.
     
  7. Atomagic

    Atomagic

    Joined:
    Aug 8, 2017
    Posts:
    1
    Thanks
     
  8. Rajdeep100

    Rajdeep100

    Joined:
    Sep 11, 2018
    Posts:
    9
    Thanks!!
     
  9. Hardcopy

    Hardcopy

    Joined:
    May 5, 2022
    Posts:
    2
    Your explanation is great.