如何从Oculus OVR摄像头进行光线投射到Unity中的鼠标位置?

时间:2015-03-10 00:52:25

标签: c# unity3d mouse raycasting oculus

我目前正在整合我用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);

我现在不知道该做什么。任何有关我出错的帮助都会非常感激。

0 个答案:

没有答案