How to make use of Gamepad controls instead Arrow Keys in Unity?

Started by
3 comments, last by frob 9 years, 7 months ago

Here is a Unity arkanoid tutorial where the paddle is controlled with arrows keys:

if (Input.GetKey(KeyCode.LeftArrow))

...
if (Input.GetKey(KeyCode.RightArrow))
...

Is it possible to change it so it uses a gamepad?

Thanks!

Advertisement

Did you look up your input manager?

Instead of pooling keycode.key, you should have something along the lines of:

Input.GetKey.Up

where Up is a definition in your input manager that refers to the gamepad.

If you want to use the pads as progressive input though, you'll need to do a bit more work.


If you want to use the pads as progressive input though, you'll need to do a bit more work.

Recommended.

It is not particularly hard to set up, so I'll take "a bit more work" as in a single bit, the smallest value.

In the Input Manager make an axis, we'll call it "Horizontal". Map horizontal positive right arrow and an alternate button of "D", with the negative button mapped to left and an alternate button of "A". Then you can create another axis with the same name mapped to a joystick axis.

That's it.

In fact, that axis is possibly already configured for you depending on how you created your project. Usually it generates the Horizontal and Vertical axis, Fire1, Fire2, Fire3 buttons, among others.

When you are done you can read the axis value as a range.

int horizontal = Input.GetAxis("Horizontal");

which gives a value between -1 and +1. If you get a -1 you move full speed to the left, +1 is full speed to the right, 0.5 is half speed, or whatever makes sense for your game.

I believe you also have the ability to simulate a 'button press' (boolean-like) interpretation of the input instead of taking the raw input (between -1 and 1 float)
Dont remember how unfortunately.
Frob, do you know the name of that option?
I remember it was in the hour and a half session hosted in Unity tutorials regarding inputs...

Input.GetButton() is probably the one you are thinking of.

if(Input.GetButton("Fire1"))

{

... do stuff...

}

This topic is closed to new replies.

Advertisement