Question

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);
    }
Was it helpful?

Solution

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;
float a = 0.1f * aNew + 0.9f * 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.

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top