using UnityEngine;
public class AccelerationTest : MonoBehaviour {
float speed = 0.2f;
Vector3 dir;
Transform mTran;
void Awake()
{
mTran = transform;
}
void OnGUI()
{
string s = string.Format("x={0},y={1},z={2}", mTran.localPosition.x, mTran.localPosition.y, mTran.localPosition.z);
GUIStyle style = new GUIStyle();
style.fontSize = 40;//设置字体大小
style.normal.background = null; //设置背景填充的
style.normal.textColor = new Color(1, 0, 0);//设置字体颜色
GUI.Label(new Rect(10, 40, 200, 20), s, style);
GUI.color = Color.red;
if (GUI.Button(new Rect(10, 20, 100, 20), "Reset")){
transform.localPosition = Vector3.zero;
}
if (GUI.Button(new Rect(200, 20, 100, 20), "Exit")){
#if UNITY_IPHONE || UNITY_ANDROID
System.Diagnostics.Process.GetCurrentProcess().Kill();
#endif
Application.Quit();
#if UNITY_EDITOR
if (Application.isEditor)
{
UnityEditor.EditorApplication.isPlaying = false;
}
#endif
}
}
// Update is called once per frame
void Update () {
//线性加速度的三维向量x、y、z分别标识手机屏幕竖直方向、水平方向和垂直屏幕方向。
//通过手机重力传感器就能获取手机移动或者旋转过程中3个分量的数值。
dir.x = -Input.acceleration.y;
dir.y = Input.acceleration.x;
dir.z = Input.acceleration.z;
//将数值大于1的线性加速度的分量限制为1
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
}
}