Potecuta
Potecuta

Reputation: 85

UnityVR - Get GameObject in center of field of view

I am trying to get the object that the user is directly looking at. Before upgrading to Unity 5 I was getting the camera object from the FPS Controller that came with the Oculus SDK and using the "camera to viewport point" or something like that to see if an object is centered on the screen. Right now this is no longer possible because they integrated the VR part in Unity. I tried using raycasts but I just can't manage to get them to go in the right direction because it looks like there are two transforms at work:

  1. The FPS controller body that is moved by the mouse
  2. The UnityVRNode which has the forward direction dictated by the movement of the headset.

For some reason I can't figure how to do it properly, and was wondering if anyone has a working piece of code for this.

Thank you!

Upvotes: 0

Views: 813

Answers (2)

Andy Rich
Andy Rich

Reputation: 1962

I've been using the center of the camera as my raycast point. (Note, to use Camera.main you need to have the camera tagged with MainCamera.) Here's how I've been doing it:

        // calculate current camera view
        var cameraCenter = new Vector3(Screen.width / 2, Screen.height / 2);
        Ray ray = Camera.main.ScreenPointToRay(cameraCenter);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            var obj = hit.collider.gameObject;
            // etc
        }

Upvotes: 0

tim
tim

Reputation: 1482

I think the best way is to send a ray from center eye from thec center eye and in the center eye's rotation. not along the forward direction.

center eye

What the first hit object is what you want object, and this is all.

Upvotes: 1

Related Questions