Vertical Move in VR using the right joystick (Quest)
The following code will enable Quest users (haven't tested with other devices) to move up and down with the right-joystick`s y axis. (the x axis being used for snap-turns).
This code will interfere with the teleport script when accidentally pointing towards an object and trying to move up. It is recommended to remove the teleport script for that matter.
You can place this script anywhere.
import { Behaviour, WebXR, GameObject} from "@needle-tools/engine";
import { Vector3,Quaternion} from "three";
import { Mathf } from "@needle-tools/engine";
export class VerticalMove extends Behaviour {
private webXR?: WebXR;
private joystickY?:number;
private worldRot: Quaternion = new Quaternion();
start(): void {
let _webxr=GameObject.findObjectOfType(WebXR);
if(_webxr)
{
this.webXR=_webxr;
console.log("webxr found");
}
}
update()
{
if(this.context.isInVR)
{
//get y value from right joystick
this.verticalMove();
}
}
verticalMove():void
{
if(this.webXR?.RightController?.input?.gamepad?.axes[3])
{
this.joystickY=this.webXR.RightController.input.gamepad.axes[3];
const speedFactor = 3;
const powFactor = 2;
const speed = Mathf.clamp01(2 * 2);
const verticalDir = this.joystickY < 0 ? 1 : -1;
let vertical = Math.pow(this.joystickY, powFactor);
vertical *= verticalDir;
vertical *= speed;
this.webXR.Rig.getWorldQuaternion(this.worldRot);
let movementVector=new Vector3();
movementVector.set(0, vertical, 0);
movementVector.applyQuaternion(this.webXR.TransformOrientation);
movementVector.x = 0;
movementVector.applyQuaternion(this.worldRot);
movementVector.multiplyScalar(speedFactor * this.context.time.deltaTime);
this.webXR.Rig.position.add(movementVector);
}
}
}