标题: unity iphone 开发入门(二) [打印本页] 作者: 奔跑的小兔 时间: 2012-4-23 13:58 标题: unity iphone 开发入门(二) 使用加速度感应检测设备方向,您随时可以使用内置的 iphoneInput.orentation 属性检测设备方向。不过,如果您需要更好的控制它,也可以使用自己的底层过滤方式处理这种情况。在加速计信号感应方面使用底层过滤方式检测设备的方向,比计算最近一次的方向平均值要好。这是因为最近值将对比旧数据视为更重要,还因为它容易执行,并且稍微更有效。(this is because recent data would be treated as more important than older data, and because it's easier to implement and it's slightly more efficient.)
private const float AccelerometerUpdateInterval = 1.0f / 60.0f; private const float LowPassFilterFactor = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; // tweakable private Vector3 lowPassValue = Vector.zero; // should be initialized with 1st sample Vector3 LowPassFilter(Vector3 newSample) { lowPassValue = Math.Lerp(lowPassValue, newSample, LowPassFilterFactor); return lowPassValue; } lowPassKernelWidthInSeconds有更大的价值,其中较慢的过滤值将靠拢当前输入采样(反之依然)。你将能够使用LowPassFilter代替avgSamples()。