Topic was automatically imported from the old Question2Answer platform.
Asked By
rogerdv
How can I rotate a mesh to face a point, but not instantly? I would like to make my characters face the destination point before walking, but in a perceptible way.
I was struggling with this too and found the following approach seems to work alright. It’s similar to the other answer posted, but without the need for an additional node. I’m still working on a better version with proper maths, rather than the “cache, set, set again” method I have happening here.
My answer is in C#, but translating to GDScript should be trivial.
// cache the current rotation
var rotation = new Quat(Rotation);
// use LookAt to look at the desired location
LookAt(targetPosition, Vector3.Up);
// cache the new "target" rotation
var targetRot = new Quat(Rotation);
// use Quat.Slerp to perform spherical interpolation to the target rotation - a weight like 0.1 works well - then set the rotation by converting the Quat back to a Euler
Rotation = rotation.Slerp(targetRot, weight).GetEuler();
Yoo, thank you soooooo much! This is exactly what I was looking for! Had to translate it to GDScript, but it was pretty easy to do so. Here is the code in GDScript if anyone wants it.
#cache the current rotation
var rot = Quat(rotation)
# use look_at to look at the desired location
look_at(target_pos, Vector3.UP)
# cache the new "target" rotation
var target_rot = Quat(rotation)
#use Quat.Slerp to perform spherical interpolation to the target rotation
#a weight like 0.1 works well
#then set the rotation by converting the Quat back to a Eule
rotation = rotation.slerp(target_rot, weight).get_euler()