Editing a bone's rotation going poorly

Godot Version

v4.3.stable.official

Question

I’m trying to get a character’s head to follow a camera as they speak into it by setting an animation key frame value using the camera for Transform3D.looking_at and adding that to animations already playing in the animation tree.

at 0,0,0, looking straight ahead, the head points the right way, but as soon as the camera moves, or the character moves, the head points anyway but where I want it to. There’s gotta be something I’m egregiously misunderstanding here. Any help would be appreciated!

extends Skeleton3D

var head_bone_index = find_bone('neck_M_03_jnt')
@export var head_target: Node3D
@export var head_up_vector: Node3D
@onready var animation_tree = %AnimationTree
@export var aim_animation: Animation

func _process(_delta: float) -> void:
	# Head Aim Constraint
	var head_position = get_bone_global_pose(head_bone_index)
	var global_head_pos = head_position * transform
	var head_target_position = head_target.global_position
	var head_lookat_transform = global_head_pos.looking_at(head_target_position, Vector3.UP, true)
	var lookat_rotation = Quaternion(head_lookat_transform.basis)
	aim_animation.track_set_key_value(0,0,lookat_rotation)

For anyone finding this thread - we ended up abandoning this approach. Instead, we brought in animations of the character tilting their head (one for x, one for y) and used a one frame animation to blend between these states in the animation tree, editing the keyframe from code.

This approach doesn’t always make the character look directly at the camera, but got very close with some remapping value finagling.

1 Like