In SceneKit, all joints of a node were individual nodes that you could add child nodes to so it syncs with the parent's animation. I would like to do the same thing in RealityKit.
The parent model I have only lists 5 children entities, not the joint used in the animation, but does have the correct joint name and transforms I need to sync with.
How do I add a child entity to a parent joint, so the child entity follows the correct path of the parent entity's animation? I.E. the parent entity has a sword swinging animation with a separate sword entity that should attach to the hand joint and move with the parent animation.
Here is the code I have. I can print the translation and it does move with the parent, but the position isn't anywhere near where the actual joint transition is (I can't see it on screen). Is this even the preferred way to do this?
This was very simple in SceneKit, just add the SCNNode as the joint's SCNNode, surely there is a simple way to do this in RealityKit?
let playerModel = try! Entity.load(named: "Idle_Normal_SingleSword").findEntity(named: "root") as! ModelEntity
playerModel.scale = SIMD3<Float>(x: 0.01, y: 0.01, z: 0.01)
let swordAnchor = AnchorEntity()
let swordModel = try! Entity.load(named: "OHS08_Sword").findEntity(named: "OHS08_Sword_R_geometry") as! ModelEntity
swordModel.scale = SIMD3<Float>(x: 0.01, y: 0.01, z: 0.01)
swordModel.orientation = simd_quatf(angle: Float(90.radians), axis: [1, 0, 0])
swordAnchor.addChild(swordModel)
initialScene.addChild(playerModel)
playerModel.addChild(swordAnchor)
let handJointIndex = playerModel.jointNames.firstIndex {
$0.contains("hand_r")
}
$0.scene.subscribe(to: SceneEvents.Update.self) { _ in
swordModel.transform.translation = playerModel.jointTransforms[handJointIndex!].translation
}
.store(in: &cancellables)