我写了一个脚本,允许用户通过查看指定的对象在两个点之间传送。具体来说,它使用光线来检查两个对象中的一个是否在屏幕的中心。它适用于标准FPSController(放置在主摄像头下)和鼠标控件。当我启用VR支持时,检测将停止工作(不仅针对中心点,而且针对整个显示器)。我应该如何添加到我的脚本中以使其与Oculus一起使用,就像使用鼠标一样?
此代码的工作原理是按名称检测对象,然后通过标记传送到指定的对象:
using UnityEngine;
using System.Collections;
using UnityEngine.VR;
public class lookTeleport : MonoBehaviour
{
public GameObject destination;
public Transform target;
RaycastHit hit;
Ray ray;
void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update ()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition); //mouse control
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.name == "teleporterA")
{
//Debug.Log("teleporter A detected");
destination = GameObject.FindWithTag("teleportY");
transform.position = destination.transform.position;
}
if (hit.collider.gameObject.name == "teleporterB")
{
//Debug.Log("teleporter B detected");
destination = GameObject.FindWithTag("teleportX");
transform.position = destination.transform.position;
}
}
}
}
我想我必须考虑用VR等效替换Input.MousePosition。
答案 0 :(得分:0)
您应该替换ray = Camera.main.ScreenPointToRay(Input.mousePosition);
ray = Camera.main.ScreenPointToRay(new Vector2(Screen.height / 2, Screen.width / 2));
由于您使用VR而不使用鼠标,Input.mousePosition
无法用作投射光线的屏幕点,相反,你可以使用用户看到的屏幕中间(如十字准线)