Controller Solutions – (Con)Tribute to Tim

ClarkGameClarkGame Posts: 74Member
edited June 2013 in Unity on OUYA
Hey OUYA Devs, >> SCROLL TO BOTTOM TO FIND THE NEWEST VERSION
or better: HEAD OVER TO THE NEW THREAD

I read a lot of questions about controllers and during the last days fought myself through the new Ouya Unity Input (and before that through the Ouya legacy input). Thanks to Tim's great support the new input framework becomes better every day. It not only works on the OUYA but also solves a lot of controller mapping issues on other platforms. Tim has done an awesome job there. Still the examples have some (minor) bugs and the setup can be somehow confusing. That's why I thought, we devs should contribute to the plugin and upload our solutions.

Here is a proposal for a nice helper script.  Right-Click to save as OuyaInput.cs to your disc.

I had a close look on Tim's ShowUnityInput.cs and the connected OuyaCommonExample.cs . In order to implement that stuff in our ongoing project I stripped these scripts down (killing all the example stuff) to a barebone static class that you can directly copy into a project and use instead of Unity's Input class. Most of this stuff is a direct copy of Tim's example, but I isolated the core code, commented it, fixed some axis invert issues and added new access methods. The static class now mirrors Unity's Input class closely on offers identical getters.

You can access the controller data via:
OuyaInput.GetAxis(string ouyaMapping, OuyaSDK.OuyaPlayer player)
OuyaInput.GetButton(OuyaSDK.OuyaPlayer player, OuyaSDK.KeyEnum ouyaButton)
OuyaInput.GetButtonDown(OuyaSDK.OuyaPlayer player, OuyaSDK.KeyEnum ouyaButton) //NEW
OuyaInput.GetButtonUp(OuyaSDK.OuyaPlayer player, OuyaSDK.KeyEnum ouyaButton) //NEW
// The new methods are returning true just for the frame the button goes down or up.
// So they mirror Unity's Input.GetButtonDown() and Input.GetButtonDown().

// In contrast to OuyaInput.GetButton() these can be used to retreive down/up EVENTS.

Examples:
// get the value for a left joystick's x-axis:
OuyaInput.GetAxis("LY", OuyaSDK.OuyaPlayer.player1)
// get the button state for a button on Ouya's O-button position (eg. XBOX360 A-button):
OuyaInput.GetButton(OuyaSDK.OuyaPlayer.player1, OuyaSDK.KeyEnum.BUTTON_O)
// get the button down event for a button on Ouya's U-button position (eg. XBOX360 X-button):
OuyaInput.GetButtonDown(OuyaSDK.OuyaPlayer.player1, OuyaSDK.KeyEnum.BUTTON_U)
// get the button up event for the click press on the right joystick:
OuyaInput.GetButtonUp(OuyaSDK.OuyaPlayer.player1, OuyaSDK.KeyEnum.R3)

In addition you want to call OuyaInput.updateJoystickList() in your FixedUpdate() method!
This makes sure that new joysticks are found or unplugging is covered.

Setup:
1. Just import the OujaSDK core package to your project.
(Plugins/Android, Ouya/SDK, Ouya/ProjectSettings, LitJson, ProjectSettings)
2. Check out the Menu >> Windows >> Open Ouya Panel and set it up (see Tim's videos).
3. Click Menu >> OUYA >> Use Ouya Project Input Settings.
4. Drop the script provided here into your project scripts.
5. ACCESS OuyaInput. methods instead of Input. methods.

Known issues:
The script is far from perfect (just a help for starters).
There still might be axis iversion issues that I did not discover (just fixed the ones i found).
GetButtonUp() and GetButtonDown() should not be called for the Triggers (they are analog and don't work based on Unty's Input.GetButtonDown() – they are treated like axis.

Credits:
This script is mostly Tim's work. But as these legacy input and mapping issues are very confusing I thought it would be good to melt it down to one  clean static class that can be used directly. I hope this script is not full of bugs and does not confuse anyone. If there are any issues with this code, please tell me and I will remove it.
Post edited by ClarkGame on
Matthias Titze
CLARK

by
GoldenTricycle


Developer for CLARK / GoldenTricycle
Developer of OuyaInput Framework >> GIT


Comments

  • ClarkGameClarkGame Posts: 74Member
    Just an addition:
    Don't forget to uncheck the Legacy Input in the OuyaGameObject.
    Matthias Titze
    CLARK

    by
    GoldenTricycle


    Developer for CLARK / GoldenTricycle
    Developer of OuyaInput Framework >> GIT


  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited June 2013
    Excellent @ClarkGame

    I had thought about adding some additional methods so it's not just a wrapper around the Unity Input API.

    I thought maybe to encourage people to use the Input Input API, the OuyaInput.cs could provide some methods like:

    // Check if the controller implements the axis/button as an Axis
    bool IsAxis(OuyaSDK.OuyaKeyCode keyCode, OuyaSDK.Player player);

    // Check if the controller implements the axis/button as an Axis
    bool IsButton(OuyaSDK.OuyaKeyCode keyCode, OuyaSDK.Player player);

    Mainly because all controllers have different mappings and you want to check if the buttons are a button or an axis. I.e. sometimes the Triggers are an Axis, and sometimes they are a button.

    And then rather than just wrap the Unity input, you could just get the keycode or axis name.

    // Get the Unity keycode that maps to the OUYA keycode
    UnityEngine.KeyCode GetKeyCode(OuyaSDK.OuyaKeyCode keyCode, OuyaSDK.Player player);

    // Get the Unity axis that maps to the OUYA keycode
    string GetAxisName(OuyaSDK.OuyaKeyCode keyCode, OuyaSDK.Player player);


    And then an actual update method would look like this:

    if (IsButton(OuyaSDK.OuyaKeyCode.BUTTON_LT, OuyaSDK.Player.Player1))
    {
       UnityEngine.KeyCode keyCode = GetKeyCode(OuyaSDK.OuyaKeyCode.BUTTON_LT, OuyaSDK.Player.Player1);
      //this part you use the Unity Input API rather than wrapping all the same functions
      bool lt = Input.GetKey(keyCode);
    }

    else if (IsAxis(OuyaSDK.OuyaKeyCode.BUTTON_LT, OuyaSDK.Player.Player1))
    {
       string axisName = GetAxisName(OuyaSDK.OuyaKeyCode.BUTTON_LT, OuyaSDK.Player.Player1);
      //this part you use the Unity Input API rather than wrapping all the same functions
      float lt = Input.GetAxisRaw(axisName);
    }

    And then the OuyaInput.cs becomes more like a mapping convenience rather than a discussion of why use your input system over the Unity input API.

    Just stuff I've been thinking about...
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    Promoted this post. I like to see more community involvement. Thanks for your hard work here...
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • EMcNeillEMcNeill Posts: 47Member
    AWESOME! Thank you both so much for making this so easy! I was already thrilled with Tim's OuyaExampleCommon.cs, and I was just sitting down today to fix the few axis invert and GetButtonDown implementation issues I had found.  :)
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    On the invert issues, I think the best way to handle it is make sure the InputManager settings have none of the axises inverted.

    And then in the mapping is the place to do the invert. Since some do it and some don't.

    That might be a method I'd add to OuyaInput.cs as well.

    // Check if the controller axis is inverted
    bool IsAxisInverted(OuyaSDK.OuyaKeyCode keyCode, OuyaSDK.Player player);

    Or you can rely on the mapper to this for you. I think the mapper should just map....
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • EMcNeillEMcNeill Posts: 47Member
    Hmm, my invert issues seem to be exacerbated by OuyaInput.cs, actually.

    I previously used the default "Use Ouya Project Input Settings" with OuyaExampleCommon.cs, and I found that the OUYA controller was correct, but both the right-stick axes were inverted on the Xbox Controller in-editor.

    When I switched to OuyaInput.cs, I found that the RX and RY axes were still inverted on the Xbox controller in-editor, but also that the OUYA controller's LY and RY axes were inverted too.
  • skytigerskytiger Posts: 14Member
    edited June 2013
    using Enum.Parse() and String.Format() at runtime isn't a good idea

    your ButtonDown() and ButtonUp() methods don't work for X360 DPAD because that gets info from Axis
    to fix that you would have to create a button state manager for each of the timelines (Update, FixedUpdate, any others)

    you need to normalize the axis inputs for playstation controllers
    you need a better controller detection
    you need a better way to detect connect/disconnect

    for a console that is defined by having a controller ... all this should have been done months ago
    Post edited by skytiger on
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited June 2013
    Your talking about non-ouya controllers. And there's a balance between using the Unity Input API  and not using it.

    The Unity Input API doesn't have a way to do controller detection other than to check the static list of joystick names which don't map to the controller #.

    There's a bit of collaboration with the Unity folks while also making it compatible with 3.X and 4.X.

    Technically Java works just fine.

    On the Enum parse, we could define a lookup table and cache that...
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • lzap-devlzap-dev Posts: 61Member
    Guys, can you please release a simple working application to check/test my OUYA controller? I mean all the buttons and also sticks? It would be great if the application could also do sounds on OUYA buttons to get audio-visual feedback on the lag...
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    The ouya-unity-plugin has a sound example where you push buttons to play an audio clip.

    And then the ShowUnityInput example has a graph plot. It's simple enough to add sounds...
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • lzap-devlzap-dev Posts: 61Member
    The ouya-unity-plugin has a sound example where you push buttons to play an audio clip.

    And then the ShowUnityInput example has a graph plot. It's simple enough to add sounds...
    I understand, the thing is if someone can upload the APK - I dont have a Windows box to compile this :-)
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    It's supposed to work on Mac too!
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • ClarkGameClarkGame Posts: 74Member
    edited June 2013
    Hey Tim,
    great that this stuff did prove somehow usefull. I think to create a corner for community contributions would be awesome – GIT could be a good place for that (although I am not really into it yet). In that way we all improve the framework, fix bugs quicker and streamline the available stuff according to our needs. In a way it's open sourcing the Ouya Unity plugin, which could be the way to go for on open console. Of course there is always the issue of bringing all these contributions together, review and test...

    For my part: I will have to finish some stuff for our presentation at E3. Afterwards I will collect all the proposals and feedback comming up in this discussion and continue my work on that convenient wrapper class. I think the methods you proposed should definately find their way into the code.

    Hey and if anyone finds the time to implement more stuff into the script: PLAESE UPLOAD your improved versions here. We also have to solve these invert issues once and for all. On my MacOSX and the OUYA the Ouya controller delivers the correct values – as does the XBOX360 controller with Tattiebogle driver.
    Post edited by ClarkGame on
    Matthias Titze
    CLARK

    by
    GoldenTricycle


    Developer for CLARK / GoldenTricycle
    Developer of OuyaInput Framework >> GIT


  • ClarkGameClarkGame Posts: 74Member
    On Invert Issues,
    I had a closer look in the issue of inverted axis. I solved mine just my testing and adjusting manually in the OuyaInput.cs class. It's not much work but not cool if you don't have all the different controllers and platforms to test.

    The mess comes from the Ouya Unity Input Settings we all use. In these settings there is a check box for axis inversion – now some are set and some are not. In the mapping methods I copied from Tim's example there are again adjustments for axis inversions (a bool added in the mapping). Well now we screw on these values twice – not good. In my underständing it's better to bring it all to one place – I would prefer the mapping method for there is a better overview there and no easy click manipulation.

    Proposal: Let's remove all the check invert checks in the Input Settings and then test all controllers on all platforms (call for the community). If we collect the results we can do the adjustments via the bool in the convinient mapping method.

    So: Up is positive / Right is positive

    Let's test and collect all data to find out which controllers on which platform have inverted axis according to this convention. For testing please remove all checks in the Input Settings and all bool flags in the mapping method of OuyaInput.cs .

    Example:
    case "LY": axisName = string.Format("Joy{0} Axis 2", (int)player); invert = true; break;
    invert = true has to be deleted in similar lines for testing.
    EMcNeill said:
    Hmm, my invert issues seem to be exacerbated by OuyaInput.cs, actually.

    I previously used the default "Use Ouya Project Input Settings" with OuyaExampleCommon.cs, and I found that the OUYA controller was correct, but both the right-stick axes were inverted on the Xbox Controller in-editor.

    When I switched to OuyaInput.cs, I found that the RX and RY axes were still inverted on the Xbox controller in-editor, but also that the OUYA controller's LY and RY axes were inverted too.

    Matthias Titze
    CLARK

    by
    GoldenTricycle


    Developer for CLARK / GoldenTricycle
    Developer of OuyaInput Framework >> GIT


  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited June 2013
    All you need to do is submit a pull request through https://github.com/ouya/ouya-unity-plugin and then it goes to me for approval.
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • skytigerskytiger Posts: 14Member
    the most recent InputManager.asset has Invert = false for all Joystick and Axis nos

  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    That's because the mapping needs to control whether an axis is inverted since the axis # changes depending on the controller...

    I started to document things, stay tuned...

    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • TyrusTyrus Posts: 67Member
    edited June 2013
    I've been trying to test out ClarkGame's OuyaInput.cs in Unity to no avail, and in troubleshooting the apparent lack of successful input I've read advice saying to uses the OUYA > "Use OUYA Project Input Settings" menu option.

    Using the latest rev of the Unity plugin I downloaded from github, I can find no such option. For me that menu has 3 "Export ___" options, 3 Copy and Paste options about various transforms, and a "Generate Unity Plugin" option.

    I tried to just overwrite my InputManager.asset file with the InputManager.asset file that comes with the Unity plugin, but that didn't seem to affect Ouya input, even though I think that's what that menu option did, any way. Is there some other thing it did that I'm missing? Feeling a bit clueless/silly here.
    Post edited by Tyrus on
  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    edited June 2013
    The OUYA->"Use OUYA Project Input Settings" became obsolete. Now when you do OUYA->"Export Core" it includes the "ProjectSettings/InputManager.asset" which you can import into your game project to achieve the old functionality.
    Post edited by tgraupmann on
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
  • TyrusTyrus Posts: 67Member
    @tgraupmann thanks! It turns out InputManager.asset was all I needed, I just had a bug I was too dumb to notice. I plan to concentrate on this error until I've felt the appropriate amount of shame.
  • ClarkGameClarkGame Posts: 74Member
    edited June 2013
    Hey OUYA Devs,
    as we are now done with our E3 stuff, I found some time to improve on the OuyaInput Framework.
    I have opened up a new thread to get a fresh start there. I think this is justified as the scripts where completely recoded, tested and expanded. Please head over to:
    NEW OuyaInput Framework


    I am still posting the framework here to close this thread with our results
    (right-click to download):

    Updated to Version_0.02

    Documentation
    Core Unity Input Access
    Barebone InputHandler (Starter Kit)
    Input Tester example (GUI overlay)
    Important Project Settings
    Everything a Package

    Hope that works for you – tell me. Cheers,
    Matthias CLARK / GoldenTricycle
    our Game >> CLARK
    Post edited by ClarkGame on
    Matthias Titze
    CLARK

    by
    GoldenTricycle


    Developer for CLARK / GoldenTricycle
    Developer of OuyaInput Framework >> GIT


  • tgraupmanntgraupmann Posts: 2,869Administrator, Team OUYA
    Sweet Awesome Clark! We have the greatest community ever; amiright!
    ~Tim Graupmann
    OUYA Inc | Android Developer
    Skype: tgraupmann_prey

    http://github.com/ouya/docs
    http://github.com/ouya/ouya-sdk-examples

    Check out the latest docs for your game engine: [setup] [adobe air] [android] [clickteam fusion] [construct 2] [corona] [libGDX] [game maker] [html5] [marmalade] [monogame] [unity] [unreal]

    Use caution when setting [persistent wireless mode].
Sign In or Register to comment.