Custom VR-Teleportation (WebXR Controllers)

Great!

I think you dont even need to derive from behaviour and dont need to add the component on an object (but yeah you can too :slightly_smiling_face: )

The class just needs to be imported somewhere (like in main.ts)

For future reference (and because I did not find anything in here and it took so much time to think of standard WebXR documentation) here’s small a script to exchange the teleport logic with a β€œmove XRRig to some fixed positions with button or trigger press”:

import { Behaviour, prefix, serializeable, WebXR, WebXREvent } from "@needle-tools/engine";
import { Object3D, WebXRController } from "three";

export class NeedleXRTeleport extends Behaviour {
    //Some Transform to Move XR Rig to
    @serializeable()
    teleportTarget: Object3D | null = null;
    private index: number = 0;

    @serializeable()
    useRotation: boolean = true;
    //XR Rig (Can be set in Editor or use GameObject.FindObjectOfType)
    @serializeable()
    xrRig: Object3D | null = null;

    //Overwrite reference Teleport implementation for Needle WebXRController, you can overwrite Raycast LineRenderer as well if you want
    @prefix(WebXRController)
    private runTeleport(rig, buttons) {
        return false;
    }

    start(): void {
        //Wait for XR Session
        WebXR.addEventListener(WebXREvent.XRStarted, () => {
            //Use Hook into standard WebXR Events for Trigger and Squeezer
            this.context.xrSession?.addEventListener("squeezestart", (event) => { this.teleportToPosition(event); });
            this.context.xrSession?.addEventListener("selectstart", (event) => { this.teleportToPosition(event); });
        });
    }
    teleportToPosition(event: XRInputSourceEvent) {
        //Do something with event, eg check handness
        if (this.xrRig && this.teleportTarget) {
            if (this.useRotation)
                this.xrRig.quaternion.copy(this.teleportTarget.quaternion);
            this.xrRig.position.copy(this.teleportTarget.position);
        }
    }
}

by user 334342083445784576

Thanks for sharing! Also thanks for sharing your usecases, always helpful to determine what steps to take when we improve the XR components.

Changed the channel name: Custom VR-Teleportation (WebXR Controllers)