To detect whether the user is actually in VR (not just on a device that supports it), donât rely solely on ('xr' in navigator) â that only tells you WebXR is available, not that a session is active. Instead, check the current XR session state.
In Needle Engine, the cleanest approach is to check the NeedleXRSession or the WebXR componentâs state. You can do something like:
const xrSession = this.context.xrSession; // or get the WebXR component
if (xrSession && xrSession.session?.isInputSourceActive) {
// VR mode
}
But the most direct and recommended pattern is to use an XRFlag-style check or hook into the XR session lifecycle. Since you want to move either the XRRig (VR) or the FPS Character/camera (desktop) on button click, the simplest is to check whether a WebXR session is currently presenting. In Needle Engine, you can access the active session via:
const webxr = this.context.scene.getComponent(WebXR);
if (webxr?.session) {
// VR is active â set XRRig position
} else {
// Desktop â set FPS character/camera position
}
Or, if youâre using DeviceUtilities, note that isQuest() returning true doesnât mean the user is in VR â they might be on the flat browser view. So thatâs not sufficient. The reliable signal is whether a WebXR session is actively presenting.
If you want your code to react automatically when the user enters or exits VR, use the XR event lifecycle â e.g., listen to sessionchange or the Needle-specific xr-session-start / xr-session-end events. That way, you can track a boolean flag and use it in your button click handler.
Tip: Since your goal is identical behavior in VR and desktop, consider making the button handler check that flag once and then move whichever object is relevant. That avoids the need to branch the teleport logic itself.
Sources: Needle Engine Docs (66%), Needle Engine Docs (63%), Needle Engine Docs (57%), Needle Engine Docs (55%)
1 replies left - Become PRO for more replies.
Reply to this post directly if you need further help (bottom right button)