Search Unity

Input keys how to reference horizontal positive/negative

Discussion in 'Scripting' started by devknob, Apr 6, 2009.

  1. devknob

    devknob

    Joined:
    Apr 2, 2009
    Posts:
    66
    Using the input Keys predefined is easy enough, but how do I reference Horizontal Positive (Right) ?

    I dont see how to add new buttons called UP DOWN RIGHT LEFT, so im trying to work with what is there. All im trying to do is get something to happen when the right left up down keys are pressed.
     
  2. Smallcode

    Smallcode

    Joined:
    Apr 6, 2009
    Posts:
    47
    Hi there!

    I've been using Input.GetAxis to capture controlled input like this:

    Code (csharp):
    1. Input.GetAxis("Vertical") > 0 // gets forward
    2. Input.GetAxis("Vertical") < 0 // gets backward
    3. Input.GetAxis("Horizontal") > 0 // gets right
    4. Input.GetAxis("Horizontal") < 0 // gets left
    There may be a best-practice way that is better than this, though.

    Also, I might have flipped the values around in the example above, so your mileage may vary ;)
     
    Duskshade and art092 like this.
  3. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    Just to further clarify, when a control is defined as an axis it has a positive and a negative part. So "Vertical" defines up as positive and down as negative. When you use GetAxis you get either a positive or a negative value, and it's up to your code to define what to do with those values.

    You can filter for positive or negative values as in Smallcode's example, but it's usually preferable to use the control's return value in a way that you don't have to do that - for example multiplying a rotation value by the axis for left/right rotation.

    If you only want one type of input from a control (and not an axis) then you can define new buttons in the Input Manager (buttons only return positive values for a given control, like "Fire" or "Jump").
     
    captainlemon1529 likes this.
  4. devknob

    devknob

    Joined:
    Apr 2, 2009
    Posts:
    66
    Im not sure if the X/Y are correct in the TransformDirection to move it specifically up or down, but I know that this should at least move the object in different directions per different keystrokes. Does anyone see why this would only move my plane in one direction, up, no matter what I change?

    if (Input.GetAxis("Vertical") > 0) {
    var a= transform.TransformDirection(0,0,50); //up
    controller.SimpleMove(a* speed);
    }
    if (Input.GetAxis("Vertical") < 0) {
    var b= transform.TransformDirection(0,50,0); // down
    controller.SimpleMove(b* speed);
    }
    if (Input.GetAxis("Horizontal") > 0) {
    var c = transform.TransformDirection(50,0,0); //right
    controller.SimpleMove(c* speed);
    }
    if (Input.GetAxis("Horizontal") < 0) {
    var d = transform.TransformDirection(-50,0,0); // left
    controller.SimpleMove(d* speed);
    }
     
  5. Diablo_programming

    Diablo_programming

    Joined:
    Nov 8, 2014
    Posts:
    1
    Change your code to look like

    if (Input.GetAxis("Vertical") > 0)
    {var a= transform.TransformDirection(0,50,0); //up
    controller.SimpleMove(a* speed);}

    if (Input.GetAxis("Vertical") < 0)
    {var b= transform.TransformDirection(0,-50,0); // down
    controller.SimpleMove(b* speed);}
    your plane was constantly moving up as your first vertical is moving on the z axis and your second vertical is constantly moving up on the y axis... also just to not lag the game set some limits on all axis
     
  6. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    57
    i know its necro post but....

    if i have this, is correct?
    Code (csharp):
    1.  
    2. float U = Input.GetAxis("Vertical") > 0;
    3.         float D = Input.GetAxis("Vertical") < 0;
    4. [\CODE]
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    No. The right hand sides represent expressions which yield booleans as a result (due to the > and < operators).
     
    Last edited: Feb 25, 2017
  8. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    57
    ooh i see.... :u

    then what are the expressions of float? :rolleyes:
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you only need the value of the axis, remove the comparison.
    If you also need comparisons, use if statements.

    Since you've already necro'd this thread => there is already shown how it works.
    Seems like you should get a little more confident with the basic syntax, types etc.

    (and next time please open a new thread)
     
  10. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    57
    yah yah Y_T
    i will, but i dont want to spam forum w my threads

    btw i founded the expression :U
     
  11. megabrobro

    megabrobro

    Joined:
    Jul 8, 2017
    Posts:
    109
  12. watercat

    watercat

    Joined:
    Dec 14, 2016
    Posts:
    13
    i have the vertical input haw to jamp with vertical positive
     
  13. Carterryan1990

    Carterryan1990

    Joined:
    Dec 29, 2016
    Posts:
    79
    crying emoji
     
  14. megabrobro

    megabrobro

    Joined:
    Jul 8, 2017
    Posts:
    109