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

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

问题描述

我正在测试Unity3D 4.3中的加速度计代码。我想要做的就是简单地在倾斜ipad的同时更改对象角度,使其像真实实况一样伪造视角。一切正常,除了加速度计过于敏感之外,即使将其放在桌子上,我也能看到GameObject就像闪烁一样。我该如何降低灵敏度,以便即使用手握住,角度也会根据倾斜度而变化,并且物体保持稳定?

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天全站免登陆