[英]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
大多數設備都具有陀螺儀,因此請嘗試使用Input.gyro.userAcceleration
請注意,在大多數Android設備上,陀螺儀默認情況下處於關閉狀態,您需要將Input.gyro.enabled
設置為true。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.