GDScript Bone LookAt in Godot 4

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By The Trope

Hi,

SkeletonModificationsStacks (Bones LookAt and IK) are currently broken in Godot 4, and I’d like to have a simple bone lookAt Target function in GDScript

I’ve been trying quite a lot of things, and either the rotation end up wrong, or offseted by a 90deg on some axis for some reason

My best attempt:

var transform = skel.get_bone_global_pose(bone2_id)
var b = (skel.global_pose_z_forward_to_bone_forward(bone2_id, transform.basis.looking_at(b2_target, Vector3(0, 1, 0)) ) )
skel.set_bone_pose_rotation(bone2_id, b)

And for some reason, it needs an offset * Basis().rotated(Vector3(0, 0, 1), -PI/2) otherwise only the transform of the bone is aligned, but the bone is not aligned with it’s transform -Z, But definitly not sure, also global_pose_z_forward_to_bone_forward is deprecated

I’m definitly missing something around the difference between local/global bone poses and maybe rest poses (also not sure about the Vector Up for the lookAt). But the documentation on Skeleton is quite limited.

Thanks for you help !