c# - unity3d - 加速度计灵敏度

标签 c# unity3d accelerometer

我正在 Unity3D 4.3 中测试加速度计代码。我想要做的是在倾斜 ipad 的同时简单地改变物体角度,就像真实的现场一样伪造视角。一切正常,除了加速度计有点过于敏感,即使我把它放在 table 上,我也能看到游戏对象在闪烁。我怎样才能让它不那么敏感,这样即使你用手握住角度也会根据倾斜度而变化并且物体保持稳定?

这是我的代码:

void Update () {
        Vector3 dir = Vector3.zero;

        dir.x = Mathf.Round(Input.acceleration.x * 1000.0f) / 1000.0f;
        dir.y = Mathf.Round(Input.acceleration.y * 1000.0f) / 1000.0f;
        dir.z = Mathf.Round(Input.acceleration.z * 1000.0f) / 1000.0f;

        // clamp acceleration vector to the unit sphere
        if (dir.sqrMagnitude > 1)
            dir.Normalize();

        // Make it move 10 meters per second instead of 10 meters per frame...
        dir *= Time.deltaTime;
        dir *= speed;

        acx = dir.x;
        acy = dir.y;
        acz = dir.z;
        transform.rotation = Quaternion.Euler(dir.y-20, -dir.x, 0);
    }

最佳答案

您可能需要使用 low pass filter (关于软件的更好描述,s. Exponential Moving Average)在使用信号输出之前。我总是使用 native 代码来获取 iPhone 上的加速度计和陀螺仪值,所以我不能 100% 确定 Unity 如何处理这个问题。但是从您所描述的内容来看,这些值似乎未经过滤。

低通滤波器根据您之前的所有值计算加权平均值。例如,过滤因子为 0.1,您的加权平均值为:

Vector3 aNew = Input.acceleration;
float a = 0.1f * aNew + 0.9f * a;

通过这种方式,您的值会以小的延迟为代价变得平滑。以 50 Hz 的频率运行加速度计,您不会注意到它。

关于c# - unity3d - 加速度计灵敏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631888/

相关文章:

2d - Unity3D:我的 2D 游戏中瓷砖之间奇怪的 1 像素白线

iphone - iOS旋转矩阵解释

c# - 使用 EditorTemplates 和单选按钮

c# - C++ dll 在 C# 代码中抛出 EntryPointNotFoundException

c# - GetComponent 函数返回 null

ios - 如何在从 iPhone 获得的加速度数据中找到所需的最大值?

iphone - 如何在 iPhone 中使用 userAcceleration

c# - 无法覆盖属性的保护集

c# - 单击 form2 上的按钮触发 form 1 中的方法

c# - 如何反序列化同一对象中具有重复属性名称的 JSON