Input keys how to reference horizontal positive/negative

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.

Hi there!

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

Input.GetAxis("Vertical") > 0 // gets forward
Input.GetAxis("Vertical") < 0 // gets backward
Input.GetAxis("Horizontal") > 0 // gets right
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 :wink:

2 Likes

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”).

1 Like

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);
}

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

i know its necro post but…

if i have this, is correct?

float U = Input.GetAxis("Vertical") > 0;
        float D = Input.GetAxis("Vertical") < 0;
[\CODE]

No. The right hand sides represent expressions which yield booleans as a result (due to the > and < operators).

ooh i see… :u

then what are the expressions of float? :roll_eyes:

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)

yah yah Y_T
i will, but i dont want to spam forum w my threads

btw i founded the expression :U

8U

i have the vertical input haw to jamp with vertical positive

crying emoji

:sunglasses: