Skip to content

Bone attachments and IK ​

Two components make characters hold and touch things:

  • BoneAttachmentComponent fixes an entity to one bone of its parent's model: a sword in a hand, a hat on the head, a backpack on the chest. It then moves with every animation.
  • IKComponent bends a character's arms or legs after the animation so a hand or foot reaches a target entity: the second hand on a rifle's grip, a hand on a door handle, a foot on a step.

Both find bones by humanoid name (rightHand, head, leftFoot …) as well as by node name, so they work on Mixamo rigs, generated auto-rigs, Unreal/UAL rigs and VRM models alike.

Managed by: ObjectManagementSystem (attachments) and IKSystem (IK, right after the AnimationSystem).

Attach an object to a bone ​

The attached entity stays a CHILD of the character (ParentId); the component names the bone. Its Transform becomes its offset from the bone: position in the character's units (metres for an unscaled character, whatever scale the rig itself uses inside), rotation relative to the bone.

Attach from a script with ObjectManagementSystem.attachToBone. By default the entity keeps its current place in the world, so place it in the hand first and then attach:

ts
import { Behaviour, type EntityInput } from "@relu-interactives/spatial-ecs";

export default class EquipSword extends Behaviour {
  data = {
    sword: { type: "entity", value: null } as EntityInput,
  };

  init() {
    const swordId = this.data.sword.value;
    if (swordId === null) return;
    const oms = this.world.getObjectManagementSystem();
    // keep the sword where it is, now riding on the right hand
    const result = oms.attachToBone(this.world, swordId, this.entityId, { humanoid: "rightHand" });
    if (!result.resolved) console.log("the model is still loading; the sword will attach when it has");
  }
}

Pass { preserveWorldTransform: false } to snap the entity onto the bone (zero offset) instead, then set its Transform as the offset.

attachToBone accepts { bone: "<node name>" } or { humanoid: "<humanoid name>" }; it fills in the other half so the attachment also resolves if the character's model is swapped for another rig. List the bones of a loaded model with listModelBones:

ts
import { Behaviour } from "@relu-interactives/spatial-ecs";

export default class LogBones extends Behaviour {
  init() {
    const bones = this.world.getObjectManagementSystem().listModelBones(this.world, this.entityId);
    for (const b of bones) console.log(b.name, b.humanoid ?? "-", "depth", b.depth);
  }
}

Detach ​

ts
import { Behaviour, type EntityInput } from "@relu-interactives/spatial-ecs";

export default class DropSword extends Behaviour {
  data = {
    sword: { type: "entity", value: null } as EntityInput,
  };

  // a "Drop" button in a spatial UI panel under this character
  onUiPress() {
    const swordId = this.data.sword.value;
    if (swordId === null) return;
    // stays where it is in the world; toRoot also unparents it from the character
    this.world.getObjectManagementSystem().detachFromBone(this.world, swordId, { toRoot: true });
  }
}

Moving an attached entity to another parent (setParent) also ends its attachment.

What happens until the model loads ​

Until the parent's model has loaded, or when the named bone doesn't exist in it, the entity sits under the character as an ordinary child. The attachment is kept and takes effect as soon as the bone appears.

Different places in different clips ​

A character rarely holds something the same way through every clip. holds say where the entity goes while particular clips play, on the attachment itself, so no script has to move it. Here a rifle rides the right hand in the rifle clips, is slung on the back in Idle, and is hidden in the rest:

ts
import { Behaviour, type EntityInput } from "@relu-interactives/spatial-ecs";
import { BoneAttachmentComponent, normalizeBoneHold } from "@relu-interactives/spatial-ecs/components";

export default class SlungRifle extends Behaviour {
  data = {
    rifle: { type: "entity", value: null } as EntityInput,
  };

  init() {
    const rifle = this.data.rifle.value;
    if (rifle === null) return;
    const attachment = this.world.getComponent(rifle, BoneAttachmentComponent);
    if (!attachment) return;
    attachment.holds = [
      { clips: ["Rifle_run", "Rifle_stand"] },
      {
        clips: ["Idle"],
        humanoid: "chest",
        position: { x: 0.1, y: 0.1, z: -0.2 },
        rotation: { x: 0, y: 0, z: 1.2 },
      },
    ].map(normalizeBoneHold);
    attachment.hideInOtherClips = true;
  }
}

The first hold that names the playing clip wins. Its fields:

FieldMeaning
clipsClip names this hold is for.
bone, humanoidAnother bone for these clips, by node name or humanoid name. Leave both null to stay on the attachment's own bone.
hiddenHide the entity in these clips.
position, rotationWhere it sits from the bone in these clips, in place of its Transform's. Leave one null to keep the Transform's. rotation is in radians.

hideInOtherClips hides the entity while a clip no hold names is playing. With no clip playing, or on a character with no Animation component, the attachment is as its own fields say and nothing is hidden. A hold never changes the entity's Transform, so saving a scene keeps it as authored. Moving between holds is immediate: there is no blend.

The clip comes from the Animation component on the entity's parent. Holds use clip names, so they follow whatever state plays that clip.

Reach for targets with IK ​

Add an IKComponent to the CHARACTER (the entity with the model) and give it one chain per limb. Each chain names the hand or foot (limb) and a target entity whose world position it reaches:

ts
import { Behaviour, type EntityInput } from "@relu-interactives/spatial-ecs";
import { IKComponent } from "@relu-interactives/spatial-ecs/components";

export default class TwoHandedGrip extends Behaviour {
  data = {
    foregrip: { type: "entity", value: null } as EntityInput,
  };

  init() {
    const grip = this.data.foregrip.value;
    if (grip === null) return;
    this.world.addComponent(
      this.entityId,
      new IKComponent({ chains: [{ limb: "leftHand", target: grip, weight: 1 }] }),
    );
  }
}

A typical rifle: the rifle is attached to the right hand, an empty child entity of the rifle marks the foregrip, and the left hand's chain targets that child, so the left hand stays on the gun through every animation.

Chain fields:

FieldMeaning
limb"leftHand", "rightHand", "leftFoot" or "rightFoot".
targetEntity id the hand/foot reaches.
poleEntity the elbow/knee bends toward; null keeps the animation's own bend.
weight0 = the animation, 1 = the target; blend in between (fade a grip in or out by animating it).
matchRotationAlso turn the hand/foot to the target's rotation.
enabledTurn one chain off.
clipsRun only while one of these clips plays; null runs whatever plays.
fadeSeconds a chain with clips takes to fade in or out when the clip changes; null follows the Animation component's cross-fade.

clips lets a character use IK only where it belongs. A left hand on a rifle's foregrip applies to the rifle clips and lets go in the rest, fading along with the clip. A chain with clips needs an Animation component on the character: with none, no clip is playing, so it stays off. One limb can have several chains with different clips, to reach different things in different clips. If a chain's clip is the first one playing, it starts on rather than fading in.

Change chains at runtime by editing chains on the component; turn all of them off with enabled = false, which puts every bone back to the animation's pose.

A limb that is dead straight has no bend of its own, so with no pole an arm out to the side drops its elbow, and a limb upright or hanging bends toward the way the character faces.

Targets out of reach are reached for as far as the limb allows. Ask the system how far a limb fell short. It answers null for a limb whose chain didn't run that frame, because the chain is off or has no target:

ts
import { Behaviour } from "@relu-interactives/spatial-ecs";

export default class ReachCheck extends Behaviour {
  onUpdate() {
    const miss = this.world.getIKSystem()?.getReachError(this.entityId, "leftHand") ?? 0;
    if (miss > 0.05) console.log(`the left hand is ${miss.toFixed(2)} m short of its target`);
  }
}