【发布时间】:2016-08-01 04:51:38
【问题描述】:
我正在为 android/ios 制作一个支持陀螺仪的应用程序,您可以在其中使用您的陀螺仪环顾四周。我想让玩家重置他们的相机位置(在设备前面重新居中场景),但我无法让系统为此工作。
这是环顾四周的代码:
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour {
void Start () {
if (SystemInfo.supportsGyroscope) {
Input.gyro.enabled = true;
//Create parent object and set this object's parent to that
GameObject camParent = new GameObject ("CamParent");
camParent.transform.position = transform.position;
transform.parent = camParent.transform;
// Rotate the parent object by 90 degrees around the x axis
camParent.transform.Rotate (Vector3.right, 90);
}
}
void Update () {
if (SystemInfo.supportsGyroscope) {
Quaternion rotation = new Quaternion (Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
transform.localRotation = rotation;
}
}
void OnGUI () {
if (SystemInfo.supportsGyroscope) {
GUILayout.Label (transform.localRotation.ToString());
GUILayout.Label (transform.parent.rotation.ToString());
if (GUILayout.Button ("Recenter View")) {
//RECENTER THE CAMERA VIEW
}
}
}
}
【问题讨论】:
标签: android ios unity3d camera gyroscope