How do I point a bone towards a point in the world in 3D?

14 Views Asked by At

I have created this function to attempt to make bones point towards targets in the world space:

func point_bone(skeleton, bone, target):
    var target_vector = skeleton.global_transform.inverse() * (target - skeleton.global_position)
    var up_vector = skeleton.global_transform.basis.y
    var v_z : Vector3 = target_vector.normalized()
    var v_x : Vector3 = up_vector.cross(-v_z)
    if !v_x.is_zero_approx():
        var new_transform = skeleton.get_bone_global_pose(bone).looking_at(
            target_vector, up_vector)
        skeleton.set_bone_global_pose_override(bone, new_transform, 1, true)

However it seems like because the bones are not facing the -z axis when imported from Blender, the wrong side of the bones points towards the location. How do I make the tip of the bone face the point instead?

0

There are 0 best solutions below