Last active
March 11, 2024 14:42
-
-
Save hobione2k/22905db48ef7d22ac092c50a03d712f6 to your computer and use it in GitHub Desktop.
ClusterScript(Beta) SubNodeをプレイヤーにくっつけるサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// インタラクトしたユーザーの頭にアイテムをくっつける | |
// ※アイテムスケールが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)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ボーンの回転に追従しないなら以下