Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Endless_Aftermath · Jul 24, 2014 at 01:19 AM · inputjoystickinput.getkeyreadonly

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;
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Endless_Aftermath · Aug 07, 2014 at 09:25 PM 0
Share

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.

avatar image Endless_Aftermath · Aug 11, 2014 at 05:41 PM 0
Share

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.

avatar image Endless_Aftermath · Sep 25, 2014 at 05:09 PM 0
Share

@$$anonymous$$ultiplayer - I'd be interested in how you coded that out if you still have it.

avatar image
3

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GODSPIRIT · Oct 23, 2020 at 07:41 AM 0
Share

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 ?

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Endless_Aftermath · Jul 24, 2014 at 04:02 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unable to catch KeyCode.LeftShift Down AND Up 5 Answers

Problems with joystick / controller axes being 1/-1 "way too often" 1 Answer

Input keycode string not working 2 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

gamepad input sensitivity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges