Unity之手机重力感应(Input.acceleration)

目录

 (一)2D重力感应

(二)3D重力感应


 (一)2D重力感应

public class image : MonoBehaviour
{
    public float speed = 10.0F;//方块移动速度
    void Update()
    {
        Vector2  dir = Vector2.zero;//方块移动向量

        dir.x = Input.acceleration.x;
        dir.y = Input.acceleration.y;

        transform.Translate(dir * speed * Time.deltaTime);
    }
}

(二)3D重力感应

public class Cube : MonoBehaviour
{
    public float speed = 10.0F;//方块移动速度
    void Update()
    {
        Vector3 dir = Vector3.zero;//方块移动向量

        dir.x = Input.acceleration.x;

        transform.Translate(dir * speed * Time.deltaTime);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40323256/article/details/88621366