How to look_at_from_position smoothly in 3D?

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

Hi Guys. In a 3rd person game I want my character’s top to look at the camera raycast’s collision point so that the character aims towards the the target that the camera is facing. I’ve got the skeleton all set up and it’s working using look_at_from_position.

When the raycast hits a collider that is closer to the camera, the top part of the skeleton abruptly changes its direction to face the new collision point.

The direction is correct but I’d like to smoothen those abrupt changes. I’ve experimented with looking_at (interpolating the direction, with the target direction being a sum of the character’s spine bone IK transform and the target’s transform) which kind of does what I want but it’s imprecise (ie, it yields different direction results than look_at_from_position).

Is there another way to smoothen look_at_from_position?

:bust_in_silhouette: Reply From: Lopy

I once dug into look_at_from_position().
static func set_direction(spatial: Spatial, direction: Vector3):
. #Reference: Godot source code
. #Reference: MESA source code
. var z := (-direction).normalized()
. var x := Vector3.UP.cross(z)
. var y := z.cross(x) #assert(x.length())
. x = x.normalized()
. y = y.normalized()
. spatial.transform.basis = Basis(x, y, z)

I translated and tweaked the function, but it should give you the value you seek. To get “direction”, just subtract your top position from the collision’s. To animate, you can use a Tween.