gpt4 book ai didi

android - Unity Gyroscope Reset Camera Position(类似oculus recenter camera)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:42 25 4
gpt4 key购买 nike

我正在为 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 Reset Camera Position(类似oculus recenter camera),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38691228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com