首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用加速度计进行滚珠运动

使用加速度计进行滚珠运动
EN

Stack Overflow用户
提问于 2015-11-01 22:13:57
回答 1查看 1.8K关注 0票数 0

我正在为Unity5.1.1f1中的Android开发迷宫游戏,我在用加速度计控制我的球时遇到了麻烦。一开始我试过:

代码语言:javascript
复制
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);
    }   
}

但是球并没有像它应该的那样移动。然后我又尝试了另一个解决方案。但我的球还是不能向右移动。似乎有时很难左/右/向前/向后移动,也有可能我的球会向相反的方向旋转。

代码语言:javascript
复制
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);
    }       
}

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-02 11:35:42

我在另一个问题中回答了这个问题:Unity 3D realistic accelerometer control --这里有两个控件变体,只需复制您认为合适的一个。

如果你有更多的问题,问。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33467828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档