# Check if user is in VR

**URL:** <https://forum.needle.tools/t/check-if-user-is-in-vr/2880>\
**Category:** 🌵 Needle Engine\
**Tags:** question\
**Created:** [September 14, 2026, 7:34am UTC](https://forum.needle.tools/t/check-if-user-is-in-vr/2880 "2026-09-14T07:34:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joostbos](https://forum.needle.tools/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@joostbos](https://forum.needle.tools/u/joostbos)\
**Post date:** [September 14, 2026, 7:34am UTC](https://forum.needle.tools/t/check-if-user-is-in-vr/2880/1 "2026-09-14T07:34:46Z")

</div>

How can I detect in my script if a user is actually in VR?

I found that I can use DeviceUtilities.isQuest() or . isDesktop() to check if the user is on the Quest or on a desktop.  
And in the same article I found a check if (‘xr’ in navigator), but that gives true when I just opened the scene and am still on the “flat” version, so before I even clicked ‘Enter VR’ on the Quest and also when I am on a desktop.

Context: I am building a sort of hotspot scene, but I want the user position to change. In VR I can use teleporting to move the XRRig, but in a desktop I have to move the FPS Character or camera by script. And I’d like to have the same behaviour in both VR and desktop, so instead of teleporting in VR I want to use a button for both. So when that button is clicked I need to know if have to set the position for the XRRig or the FPS Character/camera.

---

<div class="post-metadata">

**Author:** ![needle-assistant](https://forum.needle.tools/user_avatar/forum.needle.tools/needle-assistant/32/3415_2.png) [@needle-assistant](https://forum.needle.tools/u/needle-assistant)\
**Post date:** [September 14, 2026, 7:34am UTC](https://forum.needle.tools/t/check-if-user-is-in-vr/2880/2 "2026-09-14T07:34:46Z")

</div>

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:

```ts
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:

```ts
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](https://engine.needle.tools/docs/tutorials/scripting-walkthrough.html#:~:text=DeviceUtilities%20answers%20questions%20about%20the%20device.) (66%), [Needle Engine Docs](https://engine.needle.tools/docs/how-to-guides/scripting/examples.html#:~:text=return%20phone%3B) (63%), [Needle Engine Docs](https://engine.needle.tools/docs/reference/faq.html#:~:text=WebXR%20is%20gated%20by%20the%20xr-spatial-tracking%20permission%20policy.) (57%), [Needle Engine Docs](https://engine.needle.tools/docs/how-to-guides/xr/vr-performance.html#:~:text=VR%20has%20stricter%20performance%20requirements%20than%20desktop%20or%20mobile%20web.) (55%)

_1 replies left_ - **[Become PRO](https://needle.tools/pricing)** for more replies.  
_**Reply to this post directly if you need further help (bottom right button)**_

---

<div class="post-metadata">

**Author:** ![joostbos](https://forum.needle.tools/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@joostbos](https://forum.needle.tools/u/joostbos)\
**Post date:** [September 14, 2026, 7:50am UTC](https://forum.needle.tools/t/check-if-user-is-in-vr/2880/3 "2026-09-14T07:50:04Z")

</div>

The webxr check returns ‘undefined’ what is wrong?

---

<div class="post-metadata">

**Author:** ![needle-assistant](https://forum.needle.tools/user_avatar/forum.needle.tools/needle-assistant/32/3415_2.png) [@needle-assistant](https://forum.needle.tools/u/needle-assistant)\
**Post date:** [September 14, 2026, 7:50am UTC](https://forum.needle.tools/t/check-if-user-is-in-vr/2880/4 "2026-09-14T07:50:04Z")

</div>

The `navigator.xr` being `undefined` usually means the browser doesn’t support WebXR at all — or the page’s permissions block it (e.g., inside an iframe without `allow="xr"`). Even on a Quest, if your scene runs in an iframe where the embedding page doesn’t delegate `xr-spatial-tracking`, `navigator.xr` stays undefined.

Don’t check `navigator.xr` directly. Use Needle Engine’s built-in `WebXR` component and hook into session events instead — for example, `onEnterXR` fires when VR/AR starts. That way you get the real session status regardless of browser quirks or permission setup.

If you still need a runtime check, query `this.context.xrSession?.session` after the `WebXR` component is added. That’s `null` until a session activates.

Sources: [Needle Engine Docs](https://engine.needle.tools/docs/explanation/architecture/technical-overview.html#:~:text=Needle%20Engine%20has%20comprehensive%20WebXR%20support%2C%20enabling%20immersive%20AR%20and%20VR) (59%), [Needle Engine Docs](https://engine.needle.tools/docs/reference/faq.html#:~:text=WebXR%20is%20gated%20by%20the%20xr-spatial-tracking%20permission%20policy.) (59%), [Needle Engine Docs](https://engine.needle.tools/docs/why.html#:~:text=three.js%20is%20a%20rendering%20library.) (59%), [Needle Engine Docs](https://engine.needle.tools/docs/how-to-guides/xr/#:~:text=Build%20immersive%20AR%20and%20VR%20experiences%20that%20work%20everywhere%20%E2%80%93%20from%20Meta%20Quest%20to) (59%), [Needle Engine Docs](https://engine.needle.tools/docs/how-to-guides/xr/ios-webxr-app-clip.html#:~:text=WebXR%20Documentation%20-%20Setting%20up%20WebXR%20in%20Needle%20Engine) (59%), [Needle Engine Docs](https://engine.needle.tools/docs/#:~:text=%F0%9F%9A%80%20Deploy%20Instantly%20to%20the%20Web%20Build%20on%20your%20machine%2C%20deploy%20anywhere.) (58%), [Needle Engine Docs](https://engine.needle.tools/docs/how-to-guides/xr/vr-performance.html#:~:text=VR%20has%20stricter%20performance%20requirements%20than%20desktop%20or%20mobile%20web.) (57%), [Needle Engine Docs](https://engine.needle.tools/docs/how-to-guides/scripting/examples.html#:~:text=return%20phone%3B) (57%), [Needle Engine Docs](https://engine.needle.tools/docs/reference/changelogs/needle-engine.html#:~:text=Add%3A%20new%20Needle%20WebXR%20system) (56%), [Needle Engine Docs](https://engine.needle.tools/docs/tutorials/for-8thwall-users.html#:~:text=Needle%20Engine) (55%)

_0 replies left_ - **[Become PRO](https://needle.tools/pricing)** for more replies.  
_**Reply to this post directly if you need further help (bottom right button)**_

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [September 14, 2026, 8:30am UTC](https://forum.needle.tools/t/check-if-user-is-in-vr/2880/5 "2026-09-14T08:30:53Z")

</div>

Hello,

one easy way would be to use the onEnterXR lifecycle method in your script (just like `start` or `update` it gets called by the engine)

> **[XR Event Methods — Needle Engine](https://engine.needle.tools/docs/reference/api/xr-events.html)**
>
> Reference for WebXR AR/VR events

---

<div class="post-metadata">

**Author:** ![joostbos](https://forum.needle.tools/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@joostbos](https://forum.needle.tools/u/joostbos)\
**Post date:** [September 14, 2026, 10:41am UTC](https://forum.needle.tools/t/check-if-user-is-in-vr/2880/6 "2026-09-14T10:41:46Z")

</div>

Thanks!

Yeah, I saw that suggestion in the assistant’s answer and that works.

---

<div class="post-metadata">

**Author:** ![system](https://forum.needle.tools/user_avatar/forum.needle.tools/system/32/20_2.png) [@system](https://forum.needle.tools/u/system)\
**Post date:** [September 27, 2026, 10:42am UTC](https://forum.needle.tools/t/check-if-user-is-in-vr/2880/7 "2026-09-27T10:42:37Z")

</div>

Closed on September 27, 2026, 10:42am UTC

This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.
