Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Vibro plugin for Unity Android - Your game deserve better vibrations!

Discussion in 'Android' started by FiveFingers, Nov 7, 2011.

  1. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Native vibration plugin for Unity Android(Unity3.x, Unity 4.x Free and Pro).

    Your games deserve better vibrations!
    Available now on the Asset Store:
    Asset Store entry - Market demo
    Works with both Basic and Pro.

    iPhoneUtils.Vibrate() does vibrate for a long time on iOS and Android too.

    Where iOS doesn't expose a method to do smaller (shorter) vibrations, Android has the ability of vibrate() for a given number of milliseconds and also, play "patterns" of vibrations like morse code, or other combination in patterns with PlayPattern().

    Add cool force feedback through the device vibrator in your game or app now !
    Tired of having just a half a second vibration ?
    Try this and make morse code / rythms / light or heavy rumbles / any kind of vibration you may imagine with VibratePattern () !

    Quick Reference C#
    For initialization:
    Code (csharp):
    1.  
    2. Vibrator vibration;
    3. void Start()
    4. {
    5.         // Startup vibration Java object
    6.         vibration = new Vibrator();
    7.         vibration.Init();
    8. }
    To invoke it from anywhere:
    Code (csharp):
    1.  
    2. vibration.Vibrate ( (long) duration ); // make it vibrate for duration milliseconds
    3. vibration.VibratePattern ( (long) pattern[], (int)repeat_index); // play a pattern defined in the pattern[] array with ability of looping it at a given index)
    4. vibration.Cancel(); // stop the current vibration or vibration pattern loop (it stops also an eventual iPhoneUtils.Vibrate() call)
    5. vibration.Dispose(); // Used in OnApplicationQuit stops the vibration and destroy the library class.
    6.  
    Example:
    Code (csharp):
    1.  
    2. // make it vibrate for 150 milliseconds
    3. vibration.Vibrate ( 150 );
    4.  
    5. // make it vibrate with pattern myPattern and loop it at position repeatIndex of the pattern (-1 to shot it once)
    6. long[] myPattern  = [120,10,100,25,100,25,50,10,50,10,50,10];
    7. int repeatIndex = "5";
    8. vibration.VibratePattern ( mypattern,  repeatIndex);

    Quick Reference Javascript

    For initialization:

    Code (csharp):
    1.  
    2. static public var vibration : Vibrator;
    3. // note: if you want to use the class right away from any other javascript file and this object will be retained (DontDestroyOnLoad) other wise
    4. // static private var vibration : Vibrator;
    5.  
    6. function Start()
    7. {
    8.         // Startup vibration Java object
    9.         vibration = new Vibrator();
    10.         vibration.Init();
    11. }
    To invoke it from anywhere:

    Code (csharp):
    1.  
    2. vibration.Vibrate ( 100 ); // make it vibrate for 100 milliseconds
    3. vibration.VibratePattern ( (long) pattern[], (int)repeat_index); // play a pattern defined in the pattern[] array with ability of looping it at a given index)
    4. vibration.Cancel(); // stop the current vibration or vibration pattern loop (it stops also an eventual iPhoneUtils.Vibrate() generic call)
    5. vibration.Dispose(); // Used in OnApplicationQuit stops the vibration and destroy the library class.
    6.  
    Example:
    Code (csharp):
    1.  
    2. // make it vibrate for 150 milliseconds
    3. vibration.Vibrate ( 150 );
    4.  
    5. // make it vibrate with pattern myPattern and loop it at position repeatIndex of the pattern
    6. var myPattern : long[] = [120l,10l,100l,25l,100l,25l,50l,10l,50l,10l,50l,10l];
    7. var repeatIndex : int = "5";
    8. vibration.VibratePattern ( myPattern,  repeatIndex );(-1 to shot it once)

    Enjoy !
     

    Attached Files:

    • $nvp.png
      $nvp.png
      File size:
      37.3 KB
      Views:
      3,714
    Last edited: Jun 27, 2014
  2. _Petroz

    _Petroz

    Joined:
    May 13, 2010
    Posts:
    730
    Awesome work, it would be great to have more control over the vibration. I have a few comments though.

    "Vibrator" - is probably a poor choice for the name of a class, some will giggle some will be completely put off. Perhaps try something like "VibrationManager"

    All of the Unity inbuilt functionality measures time in seconds using floats. It would be good to stay consistent if possible.

    The VibratePattern functionality isn't very clear. Is the array 'on' 'off' 'on' so: [10, 20, 30] 10ms on, 20ms off, 30ms on or is it something different?

    You mentioned heavy and light rumbles I cannot see any interface to do that from what you posted.

    Hope this helps,
    Pete
     
  3. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    Vibration Manager somehow sounds worse than Vibrator :p
     
  4. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    @Petroz,Diviner LOL :D Ask google !
    http://developer.android.com/reference/android/os/Vibrator.html

    @Petroz About pattern, read in this page linked above, is an off on off on where you specify millisecs for each state eg: 10,100,100,10,100,50,500,50,500,25,200,25,100 and so on
    when you specify 6 as repeat index, means that it will play once the whole pattern, and then loop from given index to the end of the pattern.

    @Petroz About heavy or light rumbles, you can already see this yourself.
    On Android you can switch on small vibration for each key press, this is a <50 millisecs vibration.
    Is very light, different from a 500 ms vibration (the iPhoneUtils.Vibrate is a 500ms vibration)

    If you play well with these timings in a pattern you can achieve what I state.
    Just look at the demo of the plugin to see what I mean.

    I'm uploading an APK to the market downloadable for free for everybody to see before buying.
    I will probably put this in a game demo on the market too to show better how it works.
     
    Last edited: Nov 8, 2011
  5. _Petroz

    _Petroz

    Joined:
    May 13, 2010
    Posts:
    730
    OK that sounds great.

    LOL, I cannot believe google would call it that. Google search 'vibrator' and there are many results which could be considered unsavoury. Keep it as it is then :)

    That is what I expected but thanks for confirming it. It's great to have a low level API for this. It may be worth considering a higher level interface to easily integrate this with animations.

    OK so there is no control over intensity, only controlling duration. I misunderstood what you were saying.

    I look forward to testing out your demo. I reckon it would be good to have an in game type demo, with a complex explosion as a selling point involving multiple small vibrates. I know that's how I plan on using it so I would like to see it in that context.
     
  6. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Eheheh ok, I understand. I see what I can do.
    I already did this for my game, when something explode, but I can't upload my game yet !

    Even car and fighting type of games may find this useful, I can't find many type of game for this.
    Think about PS3 gamepad, it uses a vibrator very similar to mobiles one.

    They achieve various "intensity" by changing the duration and the frequency of the vibrator.

    I don't think they have any intensity in their SDK.
    I might be wrong, as my latest PS project was on PS2

    For now, you can download this simple demo from the market,
    https://market.android.com/details?id=com.LitobyteSoftworks.NativeVibrationDemo
    try the pattern with loop, for instance change from 3, to 5 as a starting index and see the terrible difference.
    I wrote the pattern array in 6 seconds, so imagine what we can do playing with different patterns.
     
    Last edited: Nov 8, 2011
  7. _Petroz

    _Petroz

    Joined:
    May 13, 2010
    Posts:
    730
    I just downloaded it and it works perfectly. Great work! I'll leave a review and hopefully purchase it soon.
     
  8. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Cheers mate, I prepared the three dee scene with force feedback events for you to enjoy.
    I'll publish it here as soon as approved.
     
  9. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
  10. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    I'm reviving this as we had no feedback on this, though, we found this is very interesting for Unity Android developers, so...
    Can some UA dev download this demo from the market and give us some feedback.

    Thank you!
     
  11. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    I updated the Asset Store link at the head of the thread, because was pointing to an old release.
    Sorry.
     
    Last edited: Mar 19, 2012
  12. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Quick question; How does this work on non-Android platforms?

    If I have it in my Android project and swap platforms, will it simply and cleanly not initialise or will it start throwing errors around?
     
  13. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    The bridge C# class will automatically avoid those calls if the platform selected in Unity will be NOT android.

    Hence, the java plug-in will work (make vibrations) only Android devices which support vibration.
     
  14. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Nifty, thanks :)
     
    FiveFingers likes this.
  15. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541

    At your service.
     
  16. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    We added a Javascript quick reference as well as one C# and one Javascript code example in the first post of this thread.
     
  17. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    A quick note to Unity 3.5.x and Unity 4 Android users:
    I apology for the delay. I should really update the Asset Store packages ASAP. I'll try to do it this week.
    Some user could not figure out this, so I want to add this additional info.


    The problem is about the Manifest file included in the package: Assets/Plugins/Android/AndroidManifest.xml is outdated !!
    The last version of the plugin was uploaded with Unity 3.2, before Unity Tech switched to a different method for embedding Unity player on Android. The quicker fix to this, is to delete this file, and let Unity generate the proper manifest.
    Remember to add:
    Code (csharp):
    1. <uses-permission android:name="android.permission.VIBRATE" />
    to the Unity-generated one.

    The outdated manifest file, cause the app to crash when autorotation is enabled, and you rotate the device.

    update the file Assets/Plugins/Android/AndroidManifest.xml with a copy of a Unity 3.5 / Unity 4 generated one, then add your stuff (Vibro permission and other stuff you might have added to your project). Then copy this AndroidManifest.xml file replacing the one in Assets/Plugins/Android/
    It should be it, this fixes the autorotation related and possibly other bug you might have encountered!



    Additional note:

    Before unity 3.4 AndroidManifest.xml <Application> chunk was looking like this:

    Code (csharp):
    1.   <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
    2.    
    3.   <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait">
    4.               <intent-filter>
    5.                 <action android:name="android.intent.action.MAIN" />
    6.                 <category android:name="android.intent.category.LAUNCHER" />
    7.               </intent-filter>
    8.         </activity>
    9.  </application>
    10.  


    After, it changed to be like:

    Code (csharp):
    1.  
    2.   <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
    3.     <activity android:name="com.unity3d.player.UnityPlayerProxyActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="landscape">
    4.       <intent-filter>
    5.         <action android:name="android.intent.action.MAIN" />
    6.         <category android:name="android.intent.category.LAUNCHER" />
    7.       </intent-filter>
    8.     </activity>
    9.     <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="landscape">
    10.     </activity>
    11.     <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="landscape">
    12.       <meta-data android:name="android.app.lib_name" android:value="unity" />
    13.       <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    14.     </activity>
    15.     <activity android:name="com.unity3d.player.VideoPlayer" android:label="@string/app_name" android:screenOrientation="behind" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    16.     </activity>
    17.   </application>
     
  18. Rhiojin

    Rhiojin

    Joined:
    Dec 12, 2012
    Posts:
    8
    Wow this looks super useful and 100x times easier to implement than that immersion haptic plugin :D
     
    FiveFingers likes this.
  19. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Thank you :)

    I have just mapped whatever you can do with native Android API.
    The mobile vibration motor is nothing like the Playstation controller one or such, where you have weights and dynamics that really can rumble very well, at very low frequencies.

    On a mobile phone (not to mention tablets, which usually have a weaker vibration) I don't see how to have those kind of FF functionality.

    Using very well the vibrate_pattern function, which consists of designing the array sequence of pauses/vibrations may bring some really add value to your game when you want to give explosion/rumble/hit feedback.

    Also very good for very small vibrations feedback (steps, ball bounces, etc...)

    Enjoy ;)
     
  20. tonyoakden

    tonyoakden

    Joined:
    Nov 13, 2009
    Posts:
    135
    I bought this a while ago and it worked fine with Unity 3. Now I've upgrade to Unity4 it doesn't seem to work. When I try to use the plugin to create haptic feedback on any android device it locks up and crashes. Is this a known bug? Is there a new version of the code?
    edit: It isn't a bug in the plugin. I'd made changes somewhere which caused the crash. I don't know what it is though but I rolled back to an earlier version of the code which did work and re-added my changes one at a time. My suspicion is that it wasn't linking to the java plugin properly but I don't know why, An error message would be good :)
     
    Last edited: Jun 5, 2013
    FiveFingers likes this.
  21. tonyoakden

    tonyoakden

    Joined:
    Nov 13, 2009
    Posts:
    135
    Good plugin BTW. I used it in my current Android game: Tilt and Swipe
     
    FiveFingers likes this.
  22. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Great to hear you guys make use of it.

    Unfortunately I have updated to unity 4 just two months ago to start a new project, and I didn't have really the time to test my two unity android plug-ins on Unity4. Something must be changed (a lot changed already when 3.4 was out at the Android side).
    I will investigate as soon as possible.
    The plug-in on asset store, didn't state it's for Unity4 so...
    [sarcasm]
    Since I tested Unity 4.1.0 4.1.2 4.1.3 I found so many litte and bigger problems on it, that I'm not considering it a final release yet.
    Especially for mobile, I would stick with Unity 3.5 for a while.
    [/sarcasm]

    Any info on what changed in Unity4 android side is welcome, so that I can fix this issue and update a Unity4.x version of the plug-in sooner.

    Cheers
     
unityunity