There is such a call in Unity3D

Hanheld.Vibrate(); 

When using it, the phone vibrates once. On the offsite, all without any special explanation https://docs.unity3d.com/ScriptReference/Handheld.Vibrate.html

Question - play the vibration for a longer or shorter time? What are the general possibilities of this method?

    2 answers 2

    Built-in methods in any way, but this is solved quite simply by writing a fairly simple plugin. How to create plugins: http://docs.unity3d.com/Manual/Plugins.html

    creating appropriate methods for iphone and android:

    https://stackoverflow.com/questions/12966467/are-there-apis-for-custom-vibrations-in-ios

    https://stackoverflow.com/questions/13950338/how-to-make-an-android-device-vibrate

    PS: And the attachment of the whole asset / library for just one method is a bad idea.

      Built-in Unicode. The question is solved by plugins. Simple and free in asset store = Vibration for Android

      There are many methods in it, the method for the duration of the vibration was useful to me.

       Vibration(long millis) 

      This plugin is free, but not without its flaws, in particular, it crashed on some devices when I changed the orientation of the screen.

      You can access the built-in Unity plugin using the helper classes https://docs.unity3d.com/ru/530/Manual/PluginsForAndroid.html => Using Java plug-ins with helper classes.

      In this case, the code for the vibration will be as follows.

       public static class VibratorWrapper { static AndroidJavaObject vibrator = null; static VibratorWrapper() { var unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); var unityPlayerActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"); vibrator = unityPlayerActivity.Call<AndroidJavaObject>("getSystemService", "vibrator"); } public static void Vibrate(long time) { if (HasVibrator()) vibrator.Call("vibrate", time); }} 

      Now we run from any class

       VibratorWrapper.Vibrate(2000L); 

      And vibrate for 2 seconds. Note - in the Assets / Plugins / Android folder, you must place the manifest file with the vibration right enabled. For example, like this (file is working).

        <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0"> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true"/> <application android:theme="@android:style/Theme.NoTitleBar" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" /> </activity> </application> <uses-permission android:name="android.permission.VIBRATE" />