gpt4 book ai didi

c# - 如何使用 Unity3D 检测移动设备上的摇动 Action ? C#

转载 作者:可可西里 更新时间:2023-11-01 09:02:58 24 4
gpt4 key购买 nike

我本以为 Unity 对此有一些事件触发器,但我在 Unity3d 文档中找不到。我需要改变加速度计吗?

谢谢大家

最佳答案

可以在 this thread 中找到有关检测“抖动”的精彩讨论。在 Unity 论坛上。

来自布雷迪的帖子:

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);
}
}

注意:我建议您阅读整个主题以了解完整的上下文。

关于c# - 如何使用 Unity3D 检测移动设备上的摇动 Action ? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31389598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com