统一3d根据加速度计旋转游戏对象 [英] unity 3d rotate the gameobject according to accelerometer

查看:258
本文介绍了统一3d根据加速度计旋转游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作像神庙逃亡这样的游戏.我需要根据设备倾斜的角度旋转播放器的平台.我正在尝试加速计,但无法使游戏物体倾斜. 请指导我. 谢谢

I want to make a game like temple run. I need to rotate the platform of my player according to the how much I tilt the device. I am trying the accelerometer but I could not get the game object tilted. Please guide me. Thanks

这是我的代码,我以前在注释中使用该代码,现在试图从注释中使用该代码

This is my code I was using the code in comments previously now im trying to use the code out of the comments

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);
}

}

推荐答案

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

One way of doing it to use gravity as the normal of the platform like this

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

您可能必须在加速轴上切换轴,以便根据平台上的前进方向将前进方向向下.这是我的猜测

You might have to switch the axis on the acceleration so that down is forward depending on what is forward on your platform. This is my guess

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

还请注意,这仅在设备静止时才正确.左右移动会导致平台旋转,这是不希望的.

Also note that this will only be correct when the device is still. Movement from side to side will cause a rotation on your platform which might be undesirable.

这篇关于统一3d根据加速度计旋转游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆