如何使用Unity3D检测移动设备上的抖动运动?C#
我本以为unity对此有一些事件触发器,但在Unity3d文档中找不到。我是否需要处理加速计的变化如何使用Unity3D检测移动设备上的抖动运动?C#,c#,mobile,unity3d,accelerometer,C#,Mobile,Unity3d,Accelerometer,我本以为unity对此有一些事件触发器,但在Unity3d文档中找不到。我是否需要处理加速计的变化 谢谢大家。有关检测“震动”的精彩讨论可以在Unity论坛上找到 从布雷迪的职位: 从我在苹果iPhone的一些示例应用程序中可以看出,你基本上只需设置一个向量大小阈值,在加速度计值上设置一个高通滤波器,然后如果该加速度向量的大小超过你设置的阈值,就被认为是“抖动” jmpp建议的代码(为了可读性和更接近有效的C#而修改): 注意:我建议您阅读整个线程的完整上下文 float acceleromet
谢谢大家。有关检测“震动”的精彩讨论可以在Unity论坛上找到 从布雷迪的职位: 从我在苹果iPhone的一些示例应用程序中可以看出,你基本上只需设置一个向量大小阈值,在加速度计值上设置一个高通滤波器,然后如果该加速度向量的大小超过你设置的阈值,就被认为是“抖动” jmpp建议的代码(为了可读性和更接近有效的C#而修改): 注意:我建议您阅读整个线程的完整上下文
float accelerometerUpdateInterval = 1.0f / 60.0f;
// The greater the value of LowPassKernelWidthInSeconds, the slower the
// filtered value will converge towards current input sample (and vice versa).
float lowPassKernelWidthInSeconds = 1.0f;
// This next parameter is initialized to 2.0 per Apple's recommendation,
// or at least according to Brady! ;)
float shakeDetectionThreshold = 2.0f;
float lowPassFilterFactor;
Vector3 lowPassValue;
void Start()
{
lowPassFilterFactor = accelerometerUpdateInterval / lowPassKernelWidthInSeconds;
shakeDetectionThreshold *= shakeDetectionThreshold;
lowPassValue = Input.acceleration;
}
void Update()
{
Vector3 acceleration = Input.acceleration;
lowPassValue = Vector3.Lerp(lowPassValue, acceleration, lowPassFilterFactor);
Vector3 deltaAcceleration = acceleration - lowPassValue;
if (deltaAcceleration.sqrMagnitude >= shakeDetectionThreshold)
{
// Perform your "shaking actions" here. If necessary, add suitable
// guards in the if check above to avoid redundant handling during
// the same shake (e.g. a minimum refractory period).
Debug.Log("Shake event detected at time "+Time.time);
}
}