我正在为 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
}
}
}
}
您需要添加一个原点旋转 - 将您的旋转旋转一个与您要重置的旋转相反的四元数。
在你的情况下,这将是:
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);
}
}
Quaternion origin = Quaternion.identity;
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 * origin;
}
}
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
origin = Quaternion.Inverse(transform.localRotation);
}
}
}
}
关于android - Unity Gyroscope 重置相机位置(如 oculus 重新定位相机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38691228/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |