如何使用 Unity3D 检测移动设备上的摇动动作? C#
How can I detect a shake motion on a mobile device using Unity3D? C#
我本以为 unity 有一些事件触发器,但我在 Unity3d 文档中找不到。我需要改变加速度计吗?
谢谢大家
有关检测 "shaking" 的精彩讨论可以在 Unity 论坛的 this thread 中找到。
来自布雷迪的 post:
From what I can tell in some of the Apple iPhone sample apps, you basically just set a vector magnitude threshold, set a high-pass filter on the accelerometer values, then if the magnitude of that acceleration vector is ever longer than your set threshold, it's considered a "shake".
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);
}
}
注意:我建议您阅读整个主题以了解完整的上下文。
我本以为 unity 有一些事件触发器,但我在 Unity3d 文档中找不到。我需要改变加速度计吗?
谢谢大家
有关检测 "shaking" 的精彩讨论可以在 Unity 论坛的 this thread 中找到。
来自布雷迪的 post:
From what I can tell in some of the Apple iPhone sample apps, you basically just set a vector magnitude threshold, set a high-pass filter on the accelerometer values, then if the magnitude of that acceleration vector is ever longer than your set threshold, it's considered a "shake".
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);
}
}
注意:我建议您阅读整个主题以了解完整的上下文。