using UnityEngine;
public class GyroDemo : MonoBehaviour {
// Use this for initialization
bool isGyro;
Gyroscope gyro;//陀螺仪
void Start () {
//获取设备是否支持陀螺仪,是返回true
isGyro = SystemInfo.supportsGyroscope;
//访问陀螺仪
gyro = Input.gyro;
//启用陀螺仪
gyro.enabled = true;
}
// Update is called once per frame
void Update () {
if (isGyro && gyro.enabled) {
//陀螺仪的旋转值直接应用到摄像机身上
//this.transform.rotation = gyro.attitude;
Vector3 angle = gyro.attitude.eulerAngles;
this.transform.eulerAngles = new Vector3(-angle.x, -angle.y, angle.z);// *Time.deltaTime;
this.transform.Rotate(Vector3.right * 90, Space.World);
}
}
}
Input.acceleration获取陀螺仪的重力感应
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputAdeMO : MonoBehaviour {
Vector3 speed;
// Use this for initialization
void Start () {
}
/*
手机面朝上平方(0,0,-1)
手机面朝下平放(0,0,1)
手机面朝自己,home键在下(0,-1,0)
手机面朝前面,(0,1,0)
手机面朝左边(-1,0,0)
手机面朝右边(1,0,0)
加速度:-1~1
*/
// Update is called once per frame
void Update () {
speed = Input.acceleration;
transform.Translate(new Vector3(speed.x,0,speed.y)*3*Time.deltaTime);
}
private void OnGUI()
{
GUIStyle g = new GUIStyle();
g.fontSize = 50;
GUI.Label(new Rect(0, 0, 300, 300), speed.ToString(), g);
}
}