- Home /
How do you READ or GET info from an input from a joystick/controller?
I'm looking for a read-only input check. I'm trying to create a script that let's people configure their controls via controller input. I need a way to READ what button was just pressed, that I can assign controller buttons and axis' to character movement. The problem I'm running into, is that many controllers are different and I want to have a function that sends out what button was pressed, then I can assign that button. I could probably cycle through every button mapped on a controller with "for" and "if" statements, but I would assume that Unity would have something built in to return a read-only value or string of what button was just pressed.
Does this exist or am I stuck "for-looping" through every button?
(This is NOT actual code, but a form of what I'm looking for)
OnGUI()
{
GUI.Box(new Rect(10,10,100,40),"Press a button for 'JUMP'...");
//wait for input from user
jump = Input.GetWhateverButtonWasJustPressed;
}
Answer by Multiplayer · Jul 31, 2014 at 10:02 AM
Since you asked me in my old question what I ended up doing http://answers.unity3d.com/questions/411950/find-out-if-any-button-on-any-gamepad-has-been-pre.html#answer-755604 here is the answer, but it is not pretty:
Since you can request whether a button was pressed during Update() with compound strings like "joystick 1 button 0", it is in my opinion easier to use these than do what rmaniotis suggested. So when my game starts up, I first make a list of every possible button string on every possible controller. I think the maximum number of controllers is 11. Then during each update, I test for all of these buttons and assign a function if needed.
It works flawlessly, it doesn't take much time coding since the strings can be built with a for loop, it is bug-free since you do not have to mess with the editor and in-game speed is irrelevant anyway since this is only done when changing button configurations.
Still, I feel it is kind of strange there is no way to do this similar to keyboard input.
If you still need it I can post the code that builds the strings to test for, I think I should have some for axis as well, but I do not have it on me right now.
Hey, thanks. I will use this for future projects. I appreciate it. What I was doing was trying to configure Android apps for controller input for the OUYA in an FPS. Because many controllers have different axis and button layouts programmed within them, the axis' were/are different for say a USB Xbox controller and the OUYA controller. So, what I ended up doing was looking up all of the most basic controller layouts and program$$anonymous$$g the configurations within "Player Settings". When the game loads up, it checks the names of all controllers plugged in and applies the correct button configuration based on the actual name of the controller.
So, there is a joystick command that gives the name of the joystick, then did a string search for "ouya" or "Ouya" or "OUYA" in the controller name...if it returns true, then set the controller configuration for OUYA controls....etc. It was a bit a of a pain, but it works. Your solution seems just as tedious, but I can see where your way would obtain much better and much more specific results. This is the tactic I'll use in the future. Thanks again.
How would you go about getting a string compound version of "axis"? I haven't really searched it yet, but is it as simple as "joystick 1 axis 3"? I know...my expectations are a bit high and I shouldn't expect it to be as simple as that. Haha.
@$$anonymous$$ultiplayer - I'd be interested in how you coded that out if you still have it.
Answer by Marrt · Apr 18, 2016 at 08:28 PM
i did this, this tests all possible keycodes in the enum "KeyCode" and prints if it has been pressed. This can be used to assign inputs as Keycodes, or you can cast it to strings inputs if you know how.
System.Array values = System.Enum.GetValues(typeof(KeyCode));
foreach(KeyCode code in values){
if(Input.GetKeyDown(code)){ print(System.Enum.GetName(typeof(KeyCode), code)); }
}
but i am still searching how to parse the axis...
This works really well for what I need but a syou stated the only hurdle now is figuring out how to detect input from the joystick's axes. Have you had any luck in figuring a out a way to do this ?
Answer by rmaniotis · Jul 24, 2014 at 03:31 AM
If you're just looking to read in any button from a controller, one way would be to set up your Input Manager with an input named something like "AnyButton".
Each input entry in the manager can support a main input button and an alternate input button. So each entry can support two of the buttons on the controller.
Add additional entries in the Input Manager using the same name of "AnyButton" (or your name of choice) to add more than two buttons per input name.
Once complete you, to test for this in your code, you can add:
jump = Input.GetButton("AnyButton");
or
jump = Input.GetButtonDown("AnyButton");
The first statement will get the current state of the button (pressed or not pressed) as a boolean. The second will only return true right after the button is pressed down, but subsequent calls to the function will return false until the button is released and pressed again.
Okay. I see what your saying, but still, that's a lot more work than what I was hoping for. For instance, I plan on using this button configure more than once or twice in my ga$$anonymous$$g career. Haha. This seems like a valid answer, but I thought that there would be some hidden gem in C# or Unity that just grabs an input like Input.any$$anonymous$$eyDown(), but for controller input....like Input.getAnyJoyButtonDown().
I'll try it out when I get a moment to create button presets. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154833im_/https://answers.unity.com/themes/thub/images/avi.jpg)