unity3d - Accelerometer sensitivity

耗尽温柔 提交于 2020-01-01 16:32:33

问题


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?

Here are my code:

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

回答1:


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.

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;

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




回答2:


I couldn't make Kay's example work as it was not multiplying the last part, so here's my small correction:

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


来源:https://stackoverflow.com/questions/21631888/unity3d-accelerometer-sensitivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!