【问题标题】:unity 3d rotate the gameobject according to accelerometerunity 3d 根据加速度计旋转游戏对象
【发布时间】:2014-08-18 07:16:14
【问题描述】:

我想做一个像神庙逃亡这样的游戏。我需要根据设备倾斜的程度来旋转播放器的平台。我正在尝试加速计,但我无法让游戏对象倾斜。 请指导我。 谢谢

这是我以前在 cmets 中使用代码的代码,现在我正在尝试使用 cmets 中的代码

public class tilt : MonoBehaviour {
Vector3 dir;
float movespeed;
bool istilt;
// Use this for initialization
void Start () {
    dir=Vector3.zero;
    movespeed=10.0f;
    istilt=true;

}

// Update is called once per frame
void FixedUpdate()
{
    //dir.x=-Input.acceleration.y;
    //transform.Translate(dir.x,0,0);
    //transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
    //Vector3 vec = Physics.gravity;
    /*vec.x = -Physics.gravity.z;
    vec.z = Physics.gravity.x;
    vec.y = 0;
    transform.rotation = Quaternion.Euler(vec);*/
    /*movespeed=-Input.acceleration.y;

    transform.Rotate(Vector3.up*movespeed*5.0f); 
*/float zmovment = Input.GetAxis("Horizontal");//-Input.acceleration.y;
    float xmovment = Input.GetAxis("Vertical");//-Input.acceleration.x;
    Vector3 dir = new Vector3(xmovment,0,zmovment);

    Vector3 tgravity =new Vector3(zmovment*2.0f,-15,xmovment*2.0f);
    Physics.gravity= tgravity;//new Vector3(zmovment*speed,gravity,xmovment*speed);
    Vector3 vec = Physics.gravity;
    vec.x = -Physics.gravity.z;
    vec.z = Physics.gravity.x;
    vec.y = 0;
    transform.rotation = Quaternion.Euler(vec);
}

}

【问题讨论】:

    标签: unity3d accelerometer


    【解决方案1】:

    一种使用重力作为平台法线的方法

    transform.rotation = Quaternion.LookRotation(Input.acceleration, Vector3.up);
    

    您可能需要切换加速度轴,以便根据您平台上的前进方向向下前进。这是我的猜测

    Vector3 forward = new Vector3(-Input.acceleration.y, Input.acceleration.x, Input.acceleration.z);
    transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
    

    另请注意,这仅在设备静止时才是正确的。从一侧移动到另一侧会导致您的平台发生旋转,这可能是不可取的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多