Turn an avatar's head through its humanoid rig
Updated
A Skeleton component with a humanoid rig resolves Amelia’s bones into humanoid roles. The script asks it for the head and the right arm by role, not by bone name, and makes her look around and wave.
Watch Amelia turn her head and wave; the log lists the bones the rig resolved. Drag to orbit the camera, scroll to zoom.
Turn an avatar's head through its humanoid rig
#pragma lifecycle(startup, update)
let skeleton;let head;let arm;let restArm;
// The Skeleton's humanoid rig exists once the model has loaded.function bind() { const rig = skeleton.rig; head = rig?.bone('head'); arm = rig?.bone('rightUpperArm'); if (!head || !arm) return false; restArm = arm.quaternion.clone(); const found = SkeletonComponent.humanoidBones.filter((role) => rig.bone(role)); log(`The rig resolved ${found.length} of ${SkeletonComponent.humanoidBones.length} roles: head is "${head.name}", right arm is "${arm.name}"`); return true;}
function startup() { skeleton = self.parent.getComponent(SkeletonComponent);}
const wave = new THREE.Quaternion();const parent = new THREE.Quaternion();const facing = new THREE.Vector3(0, 0, 1); // she faces +Z
function update(dt, time) { if (!head && !bind()) return; // the model is still loading head.rotation.y = Math.sin(time * 0.8) * 0.6; // Wave the right arm: a turn about her facing axis, in the arm's parent space. arm.parent.getWorldQuaternion(parent); wave.setFromAxisAngle(facing, -(2 + Math.sin(time * 5) * 0.3)); arm.quaternion.copy(parent).invert().multiply(wave).multiply(parent).multiply(restArm);}Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
RigHeadTurn.xrproj