unity3d - 加速度计灵敏度 [英] unity3d - Accelerometer sensitivity

查看:36
本文介绍了unity3d - 加速度计灵敏度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Unity3D 4.3 中测试加速度计代码.我想要做的是在倾斜 ipad 的同时简单地改变对象角度,以像真实直播一样伪造视角.一切正常,除了加速度计有点太敏感,我可以看到游戏对象就像在闪烁,即使我把它放在桌子上.我怎样才能让它不那么敏感,这样即使你用手拿着,角度也会随着倾斜而变化,物体保持稳定?

I am testing the accelerometer code in Unity3D 4.3. What I want to do is simple change the object angle while tilting the ipad, to fake view angle like real live. Everything works fine except for the fact that the accelerometer is a bit too sensitive and I can see the GameObject is like of flickering even I put it on table. How can I make it less sensitive so that even when you hold with your hand the angle will change according to the tilt and the object remain steady?

这是我的代码:

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

推荐答案

您可能需要使用 低通过滤器(s. 指数移动平均线 以更好地描述软件)之前使用信号输出.我总是使用本机代码在 iPhone 上获取加速度计和陀螺仪的值,所以我不能 100% 确定 Unity 如何处理这个.但从您所描述的情况来看,这些值似乎未经过滤.

You may need to use a low pass filter (s. Exponential Moving Average for a better description regarding software) before using the signal output. I always use native code to get accelerometer and gyroscope values on iPhone so I am not 100% sure how Unity handles this. But from what you are describing the values appear unfiltered.

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

A low pass filter calculate a weighted average from all your previous values. Having for example a filter factor on 0.1 your weighted average is:

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

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

This way your values are smoothed at the expense of a small delay. Running the accelerometer with 50 Hz you won't notice it.

这篇关于unity3d - 加速度计灵敏度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆