- Home /
Plane.Raycast to Position a mouse cursor in 3D (on Oculus)
Hello,
I would like to use a Plane.raycast to position a mouse cursor in front of the cameras of the Oculus.
I have a Camera Rig with a main camera attached to the centor anchor. So when I move the headset, the camera moves accordingly. This camera isn't actually used as a camera, but it helps alignment when I use 'camera.main.ScreenPointtoRay'
I have the following code:
void Update () {
Vector3 planePoint = camera.transform.position+camera.transform.rotation*(new Vector3(0.0f,0.0f,0.4f));
Vector3 normalVector = camera.transform.position - planePoint;
Plane mouseCollider = new Plane (normalVector, planePoint);
float rayDistance;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (mouseCollider.Raycast (ray, out rayDistance)) {
this.transform.position = ray.GetPoint (rayDistance);
}
The goal with this code is to create a plane that is 0.4 units away from the camera and renders a cursor (that is what 'this' is) at a point on the plane, according to where the mouse location is on the screen.
The thing is it mostly works. As a debug (since I can't see the plane since it isn't a gameobject) I instantiated a sphere with each mouse click. By clicking the mouse a bunch and keeping the Oculus still, I can form a point cloud and see that the spheres that I am instantiating are along the surface of a plane. And when I move the orientation of the headset, the plane that I can see via the point cloud is rotated. The issue is when I move the headset forward. At a certain point moving forward, I seem to move 'through the plane' and I can no longer see the cursor at all.
My code attempts to preserve a fixed distance of 0.4 away from the main camera (so when I move the Oculus headset forward, the cursor just moves further away (so that the cursor looks like it is the same size to the user), but it doesn't seem to be doing so. Any ideas?
BTW, I understand that there is an easier way to do this with colliders, but at this point I would like to figure it out with this method.