基于网上的方法,但是太灵敏了,修改了一下,如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CellphoneVibrate : MonoBehaviour
{
//记录上一次的重力感应的Y值
private float old_y = 0;
//记录当前的重力感应的Y值
private float new_y;
//当前手机晃动的距离
private float currentDistanceY = 0;
//手机晃动的有效距离
public float distanceY = 0.8f;
//记录上一次的重力感应的x值
private float old_x = 0;
//记录当前的重力感应的x值
private float new_x;
//当前手机晃动的距离
private float currentDistanceX = 0;
//手机晃动的有效距离
public float distanceX = 0.8f;
void Update()
{
new_y = Input.acceleration.y;
currentDistanceY = new_y - old_y;
old_y = new_y;
new_x = Input.acceleration.x;
currentDistanceX = new_x - old_x;
old_x = new_x;
if (currentDistanceY > distanceY && currentDistanceX > distanceX)
{
//实现手机晃动震动效果
//Handheld.Vibrate();
//业务逻辑代码
}
}
}