我目前正在集成我用 Oculus Rift 编写的实时战舰游戏。
当我从普通相机进行时,光线投射效果很好,但是我现在尝试从 OVRCameraRig 的 LeftEyeAnchor 光线投射到鼠标,但没有成功。这是代码的当前状态。
IEnumerator setupPlayer(){
int i = 0;
while (i < 3){
if(Input.GetKeyDown("s")){
RaycastHit hit;
Ray ray = oculusCamera.camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit, 2000)) {
Debug.Log ("Yay");
Vector3 shipSpawn = new Vector3 (hit.point.x, hit.point.y+0.6f, hit.point.z);
if(validPlayerPosition(shipSpawn)){//call test function here
shipClone = Instantiate (ship, shipSpawn, Quaternion.identity) as GameObject;
i++;
}
}
}
yield return null;
}
}
我向“s”键发送垃圾邮件,试图找到它会命中的地方,但无济于事。
然后,我编写了一个简短的脚本并将其附加到 LeftEyeAnchor 相机上,以尝试跟踪其运动,内容如下。
using UnityEngine;
using System.Collections;
public class mousetrack : MonoBehaviour {
public Camera oculusCamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = oculusCamera.camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
}
}
在运行和移动头显时,它除了沿 z 轴直线向外投射一条线外什么也没做,尽管通过移除头显我可以看到 Unity 场景中的截锥体发生了变化,但该线保持静止。我搜索了类似我自己的问题并遇到了这个线程。
https://forums.oculus.com/viewtopic.php?t=2754
我尝试了 caliberMengsk 的修复,但没有任何区别。我在每个运行时都留下了多个略有不同的错误消息实例:
Screen position out of view frustum (screen pos -1061.000000, 837.000000) (Camera rect 0 0 1036 502)
UnityEngine.Camera:ScreenPointToRay(Vector3)
mousetrack:Update() (at Assets/mousetrack.cs:13)
指向这一行:
Ray ray = oculusCamera.camera.ScreenPointToRay(Input.mousePosition);
好吧,我不知道现在该做什么。任何关于我哪里出错的帮助将不胜感激。