SoFunction
Updated on 2025-03-07

Unity realizes the shaking and vibration of mobile phone

It is often used to shake on mobile phones, and vibration can also be achieved in unity. The functions of iPhone and Android are different. The functions used in ios are () and the functions are () in Android.

Specific code:

using UnityEngine;
using ;
 
public class FunctionVibrate : MonoBehaviour
{
 //Realize the vibration effect of mobile phone // Use this for initialization
 float old_y = 0;
 float new_y;
 float max_y = 0;
 float min_y = 0;
 float d_y = 0;
 public float distance = 0.3f;
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 new_y = ;
 d_y = new_y - old_y;
 old_y = new_y;
 if(())
 {
  ();
 }
 }
 int i;
 void OnGUI() 
 {
 //if((new Rect(0,100,100,32),"vibrate!"))
 //{
 // // Vibration // ();
 //}
 (new Rect(100,100,100,100),"g:"++"d_y:"+d_y);
 (new Rect(100,200,100,100),"i:"+i);
 if(d_y>distance)
 {
  i++;
  ();
 }
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.