我使用Unity和Google VR SDK for Android开发了VR游戏。我希望游戏在没有VR耳机的情况下也可以播放。我应该如何实现从VR切换到普通模式,反之亦然?我想在普通模式下使用手机陀螺仪保持360度旋转。我在网上查看了很多脚本,但是找不到任何能够实现这一目标的东西。
我发现切换模式可以使用XRSettings.enabled = true / false(取决于模式),但是如何在Normal(非VR模式)下保持360度旋转
这是我写的脚本:
公共类GyroToggleManager:MonoBehaviour {
private int flag = 0;
private Quaternion offset;
IEnumerator SwitchToVR() {
string desiredDevice = "cardboard";
XRSettings.LoadDeviceByName(desiredDevice);
yield return null;
XRSettings.enabled = true;
transform.localRotation = Quaternion.identity;
}
IEnumerator SwitchTo2D() {
Input.gyro.enabled = true;
// couldn't figure out how to find this.
offset = ;
XRSettings.LoadDeviceByName("");
yield return null;
transform.localRotation = Quaternion.identity;
}
// Use this for initialization
void Start () {
if(XRSettings.enabled == false){
Input.gyro.enabled = true;
}
}
// Update is called once per frame
void Update () {
if (XRSettings.enabled) {
return;
}
//Also tried different combinations here nothing worked.
transform.localRotation = Input.gyro.attitude ;
}
public void StartVR(){
if(XRSettings.enabled == false){
StartCoroutine (SwitchToVR ());
}
}
public void StartN(){
if(XRSettings.enabled == true){
StartCoroutine(SwitchTo2D());
}
}
}
更新的脚本:
公共类GyroToggleManager:MonoBehaviour {
Quaternion offset;
IEnumerator SwitchToVR() {
string desiredDevice = "cardboard";
XRSettings.LoadDeviceByName(desiredDevice);
yield return null;
XRSettings.enabled = true;
transform.rotation = Quaternion.identity;
}
IEnumerator SwitchTo2D()
{
Input.gyro.enabled = true;
//Get offset.. Subtract Camera rotation from Gyro rotation
offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));
XRSettings.LoadDeviceByName("");
yield return null;
XRSettings.enabled = false;
}
private static Quaternion GyroToUnity(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
// Use this for initialization
void Start () {
if(XRSettings.enabled == false){
Input.gyro.enabled = true;
}
}
void Update()
{
if (XRSettings.enabled)
{
return;
}
//Add the gyro value with the offset then apply to the camera
transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
}
public void StartVR(){
if(XRSettings.enabled == false){
StartCoroutine (SwitchToVR ());
}
}
public void StartN(){
if(XRSettings.enabled == true){
StartCoroutine(SwitchTo2D());
}
}
}
答案 0 :(得分:1)
下面是一个简单的相机跟踪脚本,跟随玩家球,同时保持相机和播放器之间的偏移距离。它使用偏移值通过从玩家的位置减去相机的位置,然后使用Update
或LateUpdate
功能中的当前玩家位置将该偏移重新应用于相机的位置。
public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;
void Start()
{
mainCameraTransform = Camera.main.transform;
//Get camera-player Transform Offset that will be used to move the camera
cameraOffset = mainCameraTransform.position - playerTransform.position;
}
void LateUpdate()
{
//Move the camera to the position of the playerTransform with the offset that was saved in the beginning
mainCameraTransform.position = playerTransform.position + cameraOffset;
}
上面的示例和代码并不完全是您的解决方案,但它是了解您需要做什么的最简单方法。
在您的情况下,您需要从陀螺仪传感器或Input.gyro.attitude
中减去相机的旋转。微小的变化是不能真正使用-
或+
,因为两者都是Quaternion
而不是Vector3
,如上例所示。
从另一个Quaternion
减去 Quaternion
,就像我在
Start
Vector3
函数与另一个函数的倒数相乘
Quaternion
。反过来是
获得Quaternion.Inverse
。
要像Quaternions
函数一样添加两个LateUpdate
上面的Vector3
,只需将Quaternion
加在一起。
以下是代码中的相关更改:
Quaternion offset;
IEnumerator SwitchTo2D()
{
Input.gyro.enabled = true;
//Get offset.. Subtract Camera rotation from Gyro rotation
offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));
XRSettings.LoadDeviceByName("");
yield return null;
}
// Update is called once per frame
void Update()
{
if (XRSettings.enabled)
{
return;
}
//Add the gyro value with the offset then apply to the camera
transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
}
private static Quaternion GyroToUnity(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
GyroToUnity
功能用于将陀螺仪coordinate转换为Unity的坐标,然后再将其应用到相机。陀螺仪传感器使用右手坐标,而Unity的相机和其他物体使用左手坐标。有关详细信息,请参阅this。