Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Wiimote Plugin

Discussion in 'Made With Unity' started by bliprob, Oct 19, 2007.

  1. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Recently someone emailed me (can't remember who, please get in touch) and asked about the Wiimote plugin I mentioned a few months back. The release of Unity 2.0 inspired me to spend some time cleaning it up.

    The screen shot is sort of bland, but trust me, the cube is roughly matching the orientation of the wiimote in my hand:



    Here's how it works. Since Unity has no mechanism for a plugin to send events back into the engine (I really, really thought, based on zero evidence, that Unity 2.0 would support some kind of callbacks) your code has to poll the wiimote on very update. It looks like this:

    Code (csharp):
    1.  
    2.     void Start () {
    3.         wiimote_start();
    4.     }
    5.  
    6.     void OnApplicationQuit() {
    7.         wiimote_stop();
    8.     }
    9.  
    10.     void Update () {
    11.         int c = wiimote_count();
    12.         if (c>0) {
    13.             display = "";
    14.             for (int i=0; i<=c; i++) {
    15.                 int x = wiimote_getAccX(i);
    16.                 int y = wiimote_getAccY(i);
    17.                 int z = wiimote_getAccZ(i);
    18.                 float roll = Mathf.Round(wiimote_getRoll(i));
    19.                 float p = Mathf.Round(wiimote_getPitch(i));
    20.                 display += "Wiimote " + i + " accX: " + x + " accY: " + y + " accZ: " + z + " roll: " + roll + " pitch: " + p + "\n";
    21.             }
    22.         }
    23.     }
    24.  
    Although it's not shown here, there is support for the Nunchuck and the IR sensor bar. The plugin is based on DarwiinRemote (on sourceforge) with some modifications to support multiple wiimotes, the event polling (rather than callbacks), and a few other tweaks here and there.

    Plenty left to do (the phantom third Wiimote is a mystery I need to track down) but hey, it's working.
     
    Kos-Dvornik likes this.
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    That is so cool!
     
  3. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    472
    Where can we get this plugin?
    We just bought a Wii with 2 motes (not moths, mind you, those are a lot cheaper), I'd sure like to get a kick out of it while we're waiting for the SDK.
     
  4. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,120
    So, are we (Unity developers) really going to have the ability to compile for the Wii someday? :?:
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,620
    bliprob's wiimote integration into Unity is completely unrelated to Unity's Wii port that we do. If you want the actual "build for Wii" option, ask David at david@unity3d.com
     
  6. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,120
    I didn't think this Wiimote integration was related to to the official Unity port, but it did make me wonder how that port was going and if/when it is going to happen.
     
  7. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    This plugin is not related to the Unity for Wii system. I don't work for UT and I don't have the Unity Wii engine. I have spoken to David about Unity for the Wii, and it sounds great. The reason I wrote the plugin was so that I could prototype some Wii game ideas, and get funding to purchase the engine.

    In fact, I integrated the Wiimote with an engine once before -- TGE -- and the frustrations we experienced with TGE is what led to our switch to Unity. (You can download the TGE with Wiimote stuff from my blog, http://robterrell.com/ -- scroll down past the baby pictures.)

    The idea was that, once we had the game playing to our satisfaction, then we'd drop the bucks on the Unity Wii engine, either as a capex item that we'd just man up and pay, or through an outside investment. The Wii engine has a substantial additional cost (although not unreasonable, if you price out other console engines). But since then, our company received an infusion of capital (yay!) to focus on non-Wii things (boo!), so this code has sat on the shelf.

    I'll try to put the plugin on the wiki next week. I need to fix the issues with wiimote enumeration that led to the phantom extra controller. But I'll put a binary of the test app online later today, so you can try it out.

    Also, I found a Wiimote model and dropped it into the project. Here's a screenshot and a video.



    Lousy screen capture video: http://robterrell.com/WiimoteUnity.mp4
     
  8. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Awesome!!! If I were making a FPS, would it be possible to use the wiimote for navigation?
     
  9. jaydubs

    jaydubs

    Joined:
    May 3, 2007
    Posts:
    136
    That is sweeeeeet

    JW
     
  10. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Good to hear you have turned away from the "dark side." :wink:
     
  11. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Is the external plugin complied only to a .bundle - or is there a .dll for Windows too?

    Thx
    Shaun
     
  12. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Definitely. Last night I added basic Wiimote support to the FPS tutorial. The wiimote does mouselook (IR mode). It's little too sensitive to use, so I'm going to try and make it more playable today. Also I'd like to add nunchuck support for moving.
     
  13. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Great googley moogley that's cool Rob, nice work! 8)
     
  14. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Ok, wiimote IR and nunchuck support has been added to the fps tutorial. You can run through the level using the nunchuck joystick for movement (i.e. replaces / augments the WASD keys) and using the Wiimote for mouselook/aiming.

    I can make a binary build if anyone wants to try it. You'll need a wiimote and IR bar at least.
     
  15. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Uhm, triple yes I'd like to try it? :D

    Cheers,
    -Jon
     
  16. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Me too please! :D
     
  17. Bjerre

    Bjerre

    Joined:
    Nov 8, 2006
    Posts:
    108
    miiiiiiiiii too
     
  18. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    I'd like to try as well :)
     
  19. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    OK, I'll try to make a public build tonight before I lose Unity to Leopard. I just need to add some basic instructions at the start. Also, I can't recall if I made the plugin as a Universal Binary, so I need to check that.

    Tom: you took a Wiimote with you to Paris???
     
  20. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Ok, sorry about the low quality of these releases, but I rushed to get them done before installing Leopard (I need to test debug my other software on the new cat). These files are uploading now.


    Uniwii: http://robterrell.com/uniwii.zip

    Connect a Wiimote (make sure your Mac bluetooth is turned on, but no need to pair the Wiimote) and watch the on-screen Wiimote twist and turn as you move the real Wiimote. If you have an IR sensor bar, you'll see the pointer position on screen.


    FPS Tutorial with Wiimote: http://robterrell.com/fps_wiimote.app.zip

    Requires IR mode, so you need a Wii sensor bar (I use a Nyko $20 sensor bar on my Mac, or make your own as shown on my blog, http://robterrell.com). The pointer controls mouselook; the "A" button is jump, the "B" button is fire current weapon. Buttons 1 and 2 change the selected weapon. If you have a nunchuck, you can use the joystick to move around; if not, use WASD.

    Note: If you are using a Nyko or homemade sensor bar, you need to make sure you stand back the proper distance from your computer. You can't sit in front of the screen and have it work: the wiimote can't see enough IR leds.
     
  21. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    No, but I'll be home soon enough and will most definitely want to experiment with your plugin. Thus I figured it was wise to get on your list of interested parties (so you could properly gauge the level of interest). :)

    Thanks for the info above!
     
  22. MrSweet

    MrSweet

    Joined:
    Jan 19, 2007
    Posts:
    143
    Have just ordered the NYKO Bar and ready to test, this sounds remarkable :)

    IS it just a case of seting up the wireless bar and bluetooth for the Wii controller and away you go? or is there hacking to be done?

    Looking at the fps_wiimote.app.zip player it looks like won't run on 10.4, I take it this was created for 10.5?

    Many thanks

    Andy
     
  23. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    MrSweet: Both are Universal Binary player apps made for 10.4. I only just now installed Leopard and I'll test them both under 10.5 in a bit, once I get my Dock looking like normal again.

    I just place the Nyko bar atop the monitor. Turn it on, then start the Unity player. Then, press the 1 2 buttons on the Wiimote simultaneously (or press the red button inside the battery compartment). When the Wiimote is connected, there will be some text in the top-left corner of the screen that tells you the current Wiimote values.
     
  24. sLawson

    sLawson

    Joined:
    Oct 29, 2007
    Posts:
    2
    I downloaded the two files. "Uniwii" doesn't seem to recognize my wiimote. I then tried my own wiimote application that dumps data over Open Sound Control to Processing. It still seemed to connect. So, is there a trick I'm missing? Do I need to delete my wiimotes from the Bluetooth System Control Panel or some such like that? Secret mojo?

    The fps_wiimote.app.zip unzips as "Unity Player." Get info on Uniwii shows it as Universal, while Unity Player shows Intel. I'm still in PPC land for a while longer, so I can't open this one.
     
  25. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    sLawson: My apologies if it's intel only, I rushed to make those build and must have missed something. I will make a new build as soon as the Leopard Unity 2.0 is ready.

    I'm not sure why you'd be having bluetooth trouble. My guess is, I didn't make the plugin Universal. I'll check that as well.
     
  26. sLawson

    sLawson

    Joined:
    Oct 29, 2007
    Posts:
    2
    Great, looking forward to it!
     
  27. MrSweet

    MrSweet

    Joined:
    Jan 19, 2007
    Posts:
    143
    Cheers, awaiting delivery of Nyko bar.

    Well the Wiimote works fine using the uniwii player quite brilliant really ! until we get the bar I can't test the FPS, but I cant wait. The only thing we have noticed is the Wiimote model seems to shake around a little even when static like some sort of interference.

    So what are your plans ? will you be releasing the code for these neat tricks?

    Thanks

    Andy
     
  28. MrSweet

    MrSweet

    Joined:
    Jan 19, 2007
    Posts:
    143
    Hi

    Ok have the Nyko Bar, nice bit of kit and have managed to get the FPS shooter working with mouse look but the nunchuck stick does not work, I'll do a little more testing to see what i can find

    It also seems you can't mouse look that far around before it stops working only have a few inches movement left/right/up/down etc, so I guess the sensitivity needs tweaking?

    Can only get it running on an intel mac at the moment tho and you have to hit the red button a few times.

    Remarkable so far

    Thanks

    Andy
     
  29. TheLorax

    TheLorax

    Joined:
    Mar 25, 2007
    Posts:
    86
    Just wanted to check in and see what the progress is on this plugin. Are you planning on releasing it sometime soon?


    Thanks!
     
  30. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    A few things have complicated further work on this -- just personal, got-no-time issues. I did make a universal build of the plugin last week, and I finally got the new Unity installed, so I can make a new build and post it if there's interest.

    Did anyone have any feature requests? I think the only one so far has been some kind of sensitivity control for the mouselook in the FPS tutorial (which is not really on the plugin side, but rather in the mouselook code).
     
  31. MrSweet

    MrSweet

    Joined:
    Jan 19, 2007
    Posts:
    143
    Understand the time thing chap what with all those kids and a newborn :)

    I really want to give this a go and an wanting with baited breath for the release.

    Sensitivity control for the mouselook in the FPS would be cool, as you only have to move the controller a small amount and camera moves way to much, it seems the range of movement is very small, if you can post something soon then I'll post up what we are using it for :)

    Thanks again for your hard work

    Andy
     
  32. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    I hooked the sensitivity into the Wiimote "+" and "-" buttons, so you can fool with it as you like. Also, I noticed that it's important to keep the proper distance from the sensor bar, or the Wiimote's aim will seemingly invert.
     
  33. MrSweet

    MrSweet

    Joined:
    Jan 19, 2007
    Posts:
    143
    Magic! that sounds good. When are you ready to release so we can test :)

    Cheers

    Andy
     
  34. MrSweet

    MrSweet

    Joined:
    Jan 19, 2007
    Posts:
    143
  35. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    2,815
    The promise of this plugin is great, can't wait for the next update!
     
  36. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    2,815
    When do you think you will be able to release an update to support leopard and the wii mote?

    It does not fully work yet on my machine ( mbp 15" )
     
  37. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Hey Lars, sorry about the delay. I'm in the middle of a move (just got my computers set up, which of course is the first thing to do) but I will try to get the binaries online later tonight.

    By the way, there were no changes for Leopard, but if you're on a G4 you will need the latest build, as I think the original plugin wasn't a universal binary.
     
  38. MrSweet

    MrSweet

    Joined:
    Jan 19, 2007
    Posts:
    143
    Top man, thanks from me.

    We have have some interesting projects here I'll post up using your plug, really looking forward to playing as it were!

    Cheers

    Andy
     
  39. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    2,815
    That sounds super!
    Thanks

    Maybe it was not leopard related, its just that I downloaded the first person shooter demo and I only was able to fire, none of the other controls worked.
     
  40. Mike08

    Mike08

    Joined:
    Dec 29, 2005
    Posts:
    124
    Where can I download the Plugin for Unity...

    I want to give it a spin in crazy games..

    By
     
  41. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
  42. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Tom, I think he meant my plugin. Mike, I haven't released the plugin yet, but you can download and try the test apps I have posted (new universal binaries are uploading now as I type this).
     
  43. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    2,815
    Hi bliprob,

    Do you have plans to release the plugin as a beta?
     
  44. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Both test apps have been built as Universal binaries. The plugin is also now a UB, so everything should be working for you G4 users. (Sorry, can't find the box with the G4 to test with...let me know if something isn't working.)

    In the FPS app, you can change the sensitivity of the Wiimote's IR mode with the + and - buttons on the Wiimote. Remember, the FPS sample requires an IR sensor bar on your monitor. You'll also need to stand back from the monitor, as you would play the Wii on your TV. At some point I'm going to try to put the Wiimote into single-dot IR mode, which will help when you're closer in, but for now just stand back a bit.

    Links:

    Uniwii demo (rotate wiimote on-screen): http://robterrell.com/uniwii.app.zip

    FPS demo (play FPS demo RedSteel-style, with IR and nunchuck): http://robterrell.com/fps_wiimote.app.zip
     
  45. drag0nsphere

    drag0nsphere

    Joined:
    Nov 7, 2007
    Posts:
    285
    Great Work!

    By the way, when do you think you will release the plugin?
     
  46. MrSweet

    MrSweet

    Joined:
    Jan 19, 2007
    Posts:
    143
    Hello

    Any movement on this plug chap?

    Thanks

    Andy
     
  47. Loran

    Loran

    Joined:
    Nov 14, 2007
    Posts:
    124
    Rob plz, come back.
     
  48. Dragon Rider

    Dragon Rider

    Joined:
    Jan 17, 2008
    Posts:
    280
    Oh, much more than that, much MUCH more than that... For example, your WiiMote could be the gun in your hand, the steering wheel of a car, the hilt of a sword, the grip of a bow ( no idea how that would work, but hey :) ) and - my personal favorite - the handle of a lightsaber, for Star Wars® type games. I think LucasArts actually does that... Sounds pretty awesome to me too. :D
     
  49. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    (Sorry -- I have to figure out why I'm not getting emails when people post to the forums.)

    Howdy y'all, I'm still here. Lately I have been working on making a cool wiimote demo game. Also, I've been looking at making the plugin cross-platform. However, I'm working on a windows version of the htmlTexture plugin (see other thread) first, though.

    DragonRider: If you have a lightsaber model (preferable with a nice glowmap), send it my way I'll make it work with the wiimote. I'm desperate to try that, actually.

    Anyone: If you are interested in testing the existing wiimote plugin, drop me an email: rob@stinkbot.com.
     
  50. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Recently I've gotten a ton of emails asking for the UniWii plugin. I was hoping to get a chance to clean it up some before releasing it, and also finish the windows port, but to give everyone something to play with, I'll post a beta release of the Mac version.

    Here's plugin interface:
    Code (csharp):
    1.  
    2.     [DllImport ("UniWii")]
    3.     private static extern void wiimote_start();
    4.     [DllImport ("UniWii")]
    5.     private static extern void wiimote_stop();
    6.  
    7.     [DllImport ("UniWii")]
    8.     private static extern int wiimote_count();
    9.     [DllImport ("UniWii")]
    10.     private static extern bool wiimote_available( int which );
    11.     [DllImport ("UniWii")]
    12.     private static extern bool wiimote_isIRenabled( int which );
    13.     [DllImport ("UniWii")] 
    14.     private static extern bool wiimote_enableIR( int which );
    15.     [DllImport ("UniWii")]
    16.     private static extern bool wiimote_isExpansionPortEnabled( int which );
    17.     [DllImport ("UniWii")]
    18.     private static extern void wiimote_rumble( int which, float duration);
    19.     [DllImport ("UniWii")]
    20.     private static extern double wiimote_getBatteryLevel( int which );
    21.  
    22.     [DllImport ("UniWii")]
    23.     private static extern byte wiimote_getAccX(int which);
    24.     [DllImport ("UniWii")]
    25.     private static extern byte wiimote_getAccY(int which);
    26.     [DllImport ("UniWii")]
    27.     private static extern byte wiimote_getAccZ(int which);
    28.  
    29.     [DllImport ("UniWii")]
    30.     private static extern float wiimote_getIrX(int which);
    31.     [DllImport ("UniWii")]
    32.     private static extern float wiimote_getIrY(int which);
    33.     [DllImport ("UniWii")]
    34.     private static extern float wiimote_getRoll(int which);
    35.     [DllImport ("UniWii")]
    36.     private static extern float wiimote_getPitch(int which);
    37.     [DllImport ("UniWii")]
    38.     private static extern float wiimote_getYaw(int which);
    39.  
    40.     [DllImport ("UniWii")]
    41.     private static extern byte wiimote_getNunchuckStickX(int which);
    42.     [DllImport ("UniWii")]
    43.     private static extern byte wiimote_getNunchuckStickY(int which);
    44.  
    45.     [DllImport ("UniWii")]
    46.     private static extern byte wiimote_getNunchuckAccX(int which);
    47.     [DllImport ("UniWii")]
    48.     private static extern byte wiimote_getNunchuckAccZ(int which);
    49.  
    50.     [DllImport ("UniWii")]
    51.     private static extern bool wiimote_getButtonA(int which);
    52.     [DllImport ("UniWii")]
    53.     private static extern byte wiimote_getButtonB(int which);
    54.     [DllImport ("UniWii")]
    55.     private static extern byte wiimote_getButtonUp(int which);
    56.     [DllImport ("UniWii")]
    57.     private static extern byte wiimote_getButtonLeft(int which);
    58.     [DllImport ("UniWii")]
    59.     private static extern byte wiimote_getButtonRight(int which);
    60.     [DllImport ("UniWii")]
    61.     private static extern byte wiimote_getButtonDown(int which);
    62.     [DllImport ("UniWii")]
    63.     private static extern byte wiimote_getButton1(int which);
    64.     [DllImport ("UniWii")]
    65.     private static extern byte wiimote_getButton2(int which);
    66.     [DllImport ("UniWii")]
    67.     private static extern byte wiimote_getButtonNunchuckC(int which);
    68.     [DllImport ("UniWii")]
    69.     private static extern byte wiimote_getButtonNunchuckZ(int which);
    70.     [DllImport ("UniWii")]
    71.     private static extern bool wiimote_getButtonPlus(int which);
    72.     [DllImport ("UniWii")]
    73.     private static extern bool wiimote_getButtonMinus(int which);
    74.  
    So you can get the count of connected Wiimotes (I think we support up to 16, if you can think of any crazy use for that, although the plugin only lights the blue LEDs for the first four), and then get the value for a particular wiimote's sensor.

    There's no way (that I know of, anyway) for a plugin to send events into Unity's Input object, so you just have to call the plugin during Update() or FixedUpdate(), like this:
    Code (csharp):
    1.     void FixedUpdate () {
    2.         int c = wiimote_count();
    3.         if (c>0) {
    4.             display = "";
    5.             for (int i=0; i<=c-1; i++) {
    6.                 int x = wiimote_getAccX(i);
    7.                 int y = wiimote_getAccY(i);
    8.                 int z = wiimote_getAccZ(i);
    9.                 float roll = Mathf.Round(wiimote_getRoll(i));
    10.                 float p = Mathf.Round(wiimote_getPitch(i));
    11.                 float yaw = Mathf.Round(wiimote_getYaw(i));
    12.                 float ir_x = wiimote_getIrX(i);
    13.                 float ir_y = wiimote_getIrY(i);
    14.                 display += "Wiimote " + i + " accX: " + x + " accY: " + y + " accZ: " + z + " roll: " + roll + " pitch: " + p + " yaw: " + yaw + " IR X: " + ir_x + " IR Y: " + ir_y + "\n";
    15.                 if (!float.IsNaN(roll)  !float.IsNaN(p)  (i==c-1)) {
    16.                     Vector3 vec = new Vector3(p, 0 , -1 * roll);
    17.                     vec = Vector3.Lerp(oldVec, vec, Time.deltaTime * 5);
    18.                     oldVec = vec;
    19.                     GameObject.Find("wiiparent").transform.eulerAngles = vec;
    20.                 }
    21.                 if ( (i==c-1)  (ir_x != -100)  (ir_y != -100) ) {
    22.                         //float temp_x = ((ir_x + (float) 1.0)/ (float)2.0) * (float) Screen.width;
    23.                         //float temp_y = (float) Screen.height - (((ir_y + (float) 1.0)/ (float)2.0) * (float) Screen.height);
    24.                         float temp_x = ( Screen.width / 2) + ir_x * (float) Screen.width / (float)2.0;
    25.                         float temp_y = Screen.height - (ir_y * (float) Screen.height / (float)2.0);
    26.                         cursor_x = Mathf.RoundToInt(temp_x);
    27.                         cursor_y = Mathf.RoundToInt(temp_y);
    28.                 }
    29.             }
    30.         }
    31.         else display = "Press the '1' and '2' buttons on your Wii Remote.";
    32.     }
    33.  
    The plugin is at: http://robterrell.com/UniWii.bundle.zip

    Let's see what what cool stuff you guys can cook up.
     
unityunity