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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Very weird problem with Android vibrations

Discussion in 'Scripting' started by Wasplay, Apr 10, 2020.

  1. Wasplay

    Wasplay

    Joined:
    Jun 2, 2018
    Posts:
    46
    My games usually heavily use vibrations, I make complex patterns to achieve different vibration effects.
    It works really well on my device and the device of all my friends, except one !
    Vibrations makes his phone (Realme X2 Pro) lag a lot for some reasons, it makes his FPS drop from 60 to 20fps
    Is this a problem with his phone ? He don't have any problem with other games that use vibrations
    Here's my script that handles vibrations :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Vibration
    4. {
    5. #if UNITY_ANDROID && !UNITY_EDITOR
    6.  
    7.   public static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer") ;
    8.   public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity") ;
    9.   public static AndroidJavaObject vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator") ;
    10.  
    11. #else
    12.     public static AndroidJavaClass unityPlayer;
    13.     public static AndroidJavaObject currentActivity;
    14.     public static AndroidJavaObject vibrator;
    15. #endif
    16.     public static void Vibrate()
    17.     {
    18.         if (isAndroid())
    19.             vibrator.Call("vibrate");
    20.         else
    21.             Handheld.Vibrate();
    22.     }
    23.     public static void Vibrate(long milliseconds)
    24.     {
    25.         if (isAndroid())
    26.             vibrator.Call("vibrate", milliseconds);
    27.         else
    28.             Handheld.Vibrate();
    29.     }
    30.     public static void Vibrate(long[] pattern, int repeat)
    31.     {
    32.         if (isAndroid())
    33.             vibrator.Call("vibrate", pattern, repeat);
    34.         else
    35.             Handheld.Vibrate();
    36.     }
    37.     public static bool HasVibrator()
    38.     {
    39.         return isAndroid();
    40.     }
    41.     public static void Cancel()
    42.     {
    43.         if (isAndroid())
    44.             vibrator.Call("cancel");
    45.     }
    46.     private static bool isAndroid()
    47.     {
    48. #if UNITY_ANDROID && !UNITY_EDITOR
    49.     return true ;
    50. #else
    51.         return false;
    52. #endif
    53.     }
    54. }
     
    Last edited: Apr 10, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Use some Debug.Log() calls inside the above wrappers to make sure you're not calling into Java like a bazillion times per frame. Calling any of those Java objects probably has a lot of interop overhead, so if you mix up where you are calling them, you might be needlessly calling the same function way too many times. At least you can eliminate that.
     
  3. Wasplay

    Wasplay

    Joined:
    Jun 2, 2018
    Posts:
    46
    Some effects require me to call the Vibration object up to 10 times per second, but that's the maximum.
    The problem only happens on the Realme X2 Pro, a high-end gaming phone, which is very weird !
    It's probably due to their "4D Vibrations" feature but I don't think there's anything I can do about it :(

    The phone I use to test my game is a Honor 6X which is a low-end early 2017 phone, and the game runs at a pretty constant 60fps on it.
     
  4. prabhukaran

    prabhukaran

    Joined:
    May 5, 2019
    Posts:
    13
    I also having the problem. Please someone help in this.
    Creating fps problem in my OnePlus Nord device but working smoothly in other lower end devices