Skip to content

Instantly share code, notes, and snippets.

@hobione2k
Last active March 11, 2024 14:42
Show Gist options
  • Save hobione2k/22905db48ef7d22ac092c50a03d712f6 to your computer and use it in GitHub Desktop.
Save hobione2k/22905db48ef7d22ac092c50a03d712f6 to your computer and use it in GitHub Desktop.
ClusterScript(Beta) SubNodeをプレイヤーにくっつけるサンプル
// インタラクトしたユーザーの頭にアイテムをくっつける
// ※アイテムスケールが1であること
$.onInteract((player) => {
$.state.targetPlayer = player;
});
const attachSubNodeToPlayer = (subNode, player, bone, offset) => {
const bonePos = player.getHumanoidBonePosition(bone);
const boneRot = player.getHumanoidBoneRotation(bone);
const globalPos = bonePos.add(offset.clone().applyQuaternion(boneRot));
// 親オブジェクトを原点とした相対座標に変換する
const parentPos = $.getPosition(); // 親がサブノードがの場合はここをparentNode.getGlobalPosition()に置き換える
const parrentRotInv = $.getRotation().invert(); // 親がサブノードがの場合はここをparentNode.getGlobalRotation().invert()に置き換える
const position = globalPos.sub(parentPos).applyQuaternion(parrentRotInv);
const rotation = boneRot.multiply(parrentRotInv);
subNode.setPosition(position);
subNode.setRotation(rotation);
};
const SubNodeObject = $.subNode("SubNodeObject");
$.onUpdate((deltaTime) => {
const player = $.state.targetPlayer;
if(player!=null && player.exists()) {
// サブノード、対象プレイヤー、頭ボーン、オフセットを指定
attachSubNodeToPlayer(SubNodeObject, player, HumanoidBone.Head, new Vector3(0, 0.5, 0));
}
});
@hobione2k
Copy link
Author

ボーンの回転に追従しないなら以下

$.onInteract((player) => {
  $.state.targetPlayer = player;
});

const attachSubNodeToPlayer = (subNode, player, bone, offset) => {
  const bonePos = player.getHumanoidBonePosition(bone);
  const globalPos = bonePos.add(offset.clone().applyQuaternion(boneRot));

  // 親オブジェクトを原点とした相対座標に変換する
  const parentPos = $.getPosition();               // 親がサブノードがの場合はここをparentNode.getGlobalPosition()に置き換える
  const parrentRotInv = $.getRotation().invert();  // 親がサブノードがの場合はここをparentNode.getGlobalRotation().invert()に置き換える
  const position = globalPos.sub(parentPos).applyQuaternion(parrentRotInv);

  subNode.setPosition(position);
};

const SubNodeObject = $.subNode("SubNodeObject");

$.onUpdate((deltaTime) => {
  const player = $.state.targetPlayer;
  if(player!=null && player.exists()) { 
    // サブノード、対象プレイヤー、頭ボーン、オフセットを指定
    attachSubNodeToPlayer(SubNodeObject, player, HumanoidBone.Head, new Vector3(0, 0.5, 0));
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment