使用加速度计进行滚球运动

Use Accelerometer for Roll-a-ball Movement

我正在 Unity 5.1.1f1 中为 Android 开发迷宫游戏,我在使用加速度计控制我的球时遇到了问题。一开始我试过:

public class PlayerController : MonoBehaviour {

    public GameObject sphere;
    public Camera camera;
    public float speed=200;

    private Rigidbody myRigidBody;

    void Start()
    {
        myRigidBody = gameObject.GetComponent<Rigidbody> ();
    }


    void FixedUpdate() 
    {
        float moveH = Input.acceleration.x;
        float moveV = -Input.acceleration.z;

        Vector3 move = new Vector3 (moveH, 0.0f, moveV);
        myRigidBody.AddForce (move * speed*Time.deltaTime);
    }   
}

但是球没有按预期移动。然后我尝试了另一种解决方案。但是我的球仍然没有正确移动。好像有时很难移动left/right/forward/backward,而且我的球可能会向相反的方向旋转。

public class PlayerController : MonoBehaviour {


    public float speedAc = 10;

    //accelerometer
    private Vector3 zeroAc;
    private Vector3 curAc;
    private float sensH = 10;
    private float sensV = 10;
    private float smooth = 0.5f;
    private float GetAxisH = 0;
    private float GetAxisV = 0;

    // Use this for initialization
    void Start () {

        ResetAxes();
    }

    //accelerometer
    void ResetAxes(){
        zeroAc = Input.acceleration;
        curAc = Vector3.zero;
    }

    void FixedUpdate () {

        curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);

        GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 1);
        GetAxisV = Mathf.Clamp(-curAc.z * sensV, -1, 1);

        Vector3 movement = new Vector3 (GetAxisH, 0.0f, GetAxisV);
        GetComponent<Rigidbody>().AddForce(movement * speedAc);
    }       
}

有人可以帮帮我吗?

我已经在另一个 SO 问题中回答了这个问题: - 你有两种控制变体,只需复制你认为合适的那个。

如果您有更多问题,请询问。