加速度计检测旋转而不是加速度
Accelerometer detects rotation instead of acceleration
我正在尝试让设备统一加速以移动对象
public class ExampleClass : MonoBehaviour {
public float speed = 10.0F;
void Update() {
Vector3 dir = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// Move object
transform.Translate(dir * speed);
}
}
但是当我 运行 在我的设备上玩游戏时,对象移动和停止取决于设备的方向而不是加速度。
我也尝试打印 Input.acceleration
读数
GUI.Button(new Rect(10, 10, 150, 80), Input.acceleration.x + "\n" + Input.acceleration.y + "\n" + Input.acceleration.z);
而且我注意到这三个数字的值只有在我旋转设备时才会改变,而且它们的值变化总是-1和1。
我知道加速度计是用来测量加速度的,不是旋转的。测量旋转的传感器是陀螺仪。
为什么会这样?我如何读取加速度而不是旋转。
现在大多数设备都有陀螺仪所以试试Input.gyro.userAcceleration
请注意,在大多数 android 设备上,陀螺仪默认处于关闭状态,您需要将 Input.gyro.enabled
设置为 true。
我正在尝试让设备统一加速以移动对象
public class ExampleClass : MonoBehaviour {
public float speed = 10.0F;
void Update() {
Vector3 dir = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// Move object
transform.Translate(dir * speed);
}
}
但是当我 运行 在我的设备上玩游戏时,对象移动和停止取决于设备的方向而不是加速度。
我也尝试打印 Input.acceleration
读数
GUI.Button(new Rect(10, 10, 150, 80), Input.acceleration.x + "\n" + Input.acceleration.y + "\n" + Input.acceleration.z);
而且我注意到这三个数字的值只有在我旋转设备时才会改变,而且它们的值变化总是-1和1。
我知道加速度计是用来测量加速度的,不是旋转的。测量旋转的传感器是陀螺仪。
为什么会这样?我如何读取加速度而不是旋转。
现在大多数设备都有陀螺仪所以试试Input.gyro.userAcceleration
请注意,在大多数 android 设备上,陀螺仪默认处于关闭状态,您需要将 Input.gyro.enabled
设置为 true。