Setting 'Target Eye' on Camera, at runtime (cant seem to do it)

Im wanting to have both VR/Non-VR in game. And for my canvas raycasters to work right in each. (2020.3.23f1, URP)

This seems to work well enough as it is (XR Managemnet, VR in unity in general), except for one thing: Target Eye Mode + those raycasters.

In NON VR, if TargetEye on the main camera is set to ‘Both’ (the default), none of the raycasters work right. The UI shows on the left eye by default, and the raycasters hit … things, but nothing lines up with what you see.

If you manually switch the main camera targetEye (at runtime) to None, everything works. Suddenly the fov in player is large (90 deg, since that hasn’t changed) and the raycasters work as expected.

However I cannot find a way to change targetEye from a script.
When I do, it’s ignored. This is true if I’m trying to change it from Both → None (the “Im not in VR” scenario) and also if I change it manually to None (in editor), pop on a headset, and have the code try to change it from None → Both (the “Im in VR, honest!” mode).

Digging through the docs I also see a:
XRSettings.gameViewRenderMode

I’ve tried setting that as well, still no effect.

Is there a way to dynamically change the targetEye, at runtime?
If you do it manually, in the editor, while the game is running - it does work. So there’s gotta be a way… right?

        private void SetupCamerasForVRAndNonVR(bool hmdEnabled)
        {
            var playerCamera = _worldUICoordinator.PlayerCamera();
            if (!hmdEnabled)
            {
                XRSettings.gameViewRenderMode = GameViewRenderMode.None;
                playerCamera.stereoTargetEye = StereoTargetEyeMask.None;
            }
            else
            {
                // main camera needs to be setup for both eyes.
                XRSettings.gameViewRenderMode = GameViewRenderMode.BothEyes;
                playerCamera.stereoTargetEye = StereoTargetEyeMask.Both;
            }
        }

Did you happen to find a solution?

Nope. Unfortunately

I also have this issue, for now I think the best solution is to have two camera’s and switch between them.

That issue is still there. Software side switching does not work in URP but it works on the legacy Renderer.
switching between two cameras works perfectly fine however. So at least that worked :slight_smile:

So Unity created a private field that duplicates the exact purpose of a pre-existing public field, disconnected the public field from any and all functionality, gave us no ability to alter or read the actual behavior of the camera, and left the old useless field intact with no documentation explaining what they had done.

Genius.

That tracks…

Good idea with two cameras, Thanks @Mubanga ! I’m on URP as well…

They probably made it a [SerializeField] private…

Camera.main.GetComponent<UnityEngine.Rendering.Universal.UniversalAdditionalCameraData>().allowXRRendering = false;
This works.

1 Like

[URP Unity 6000.0.22f1] This is only solution which is actually working, so:

        monitorCam.targetDisplay = 0;
        monitorCam.fieldOfView = 60f;
        monitorCam.enabled = true;
        monitorCam.stereoTargetEye = StereoTargetEyeMask.None;
        monitorCam.GetComponent<UnityEngine.Rendering.Universal.UniversalAdditionalCameraData>().allowXRRendering = false;

        xrCamera.targetDisplay = 0;
        xrCamera.enabled = true;
        xrCamera.stereoTargetEye = StereoTargetEyeMask.Both; 

That works for sure, so I walk around in headset and on monitor I got view from the spectator camera.

No solutions from any LLMs workd, it’s not enough you manipulate targetDisplay/depth/stereoTargetEye, you need to set allowXRRendering