Godot's look_at function is lovely, I enjoy using it and appreciate it. However, I very often find myself wishing it returned a product instead of operating in place.
This is a problem because when we want to smooth an objects motion with interpolation, the object has already reached the destination when we receive the result if I use this function.
Simple look at tracking looks like this, every frame we look at the target's position from our object's current position.
This works fine and is perfectly serviceable if not exactly what I am looking for most of the time. Our object snaps directly to looking at the target regardless of where it is or how fast it would have to move.
We can create a smooth interpolation like this by manually calculating the angles to the target point. But we can also leverage the calculations Godot is already doing for us instead of reimplementing maths ourselves.
To do this we are going to create a cache for our current transform. Before we look at the target position we update the cache with our current transform. Finally we interpolate our cached transform with our current transform and assign the result back to our current transform.
The object is now tracking our target point at a smooth consistent speed.
This works because we are treating the current position of the object as a cache for its destination and storing the actual position of the object before we move it. Then applying a standard interpolation between these points.
You may be wondering why I am caching the whole transform instead of just the rotation. The primary reason is that lerping Euler angles in Godot is a pain.
The secondary reason is also why I have been using look_at_from_position in these code snippets instead of look_at.
If I feed in the target's position minus the direction to the target's position from our object's current position as the position our object is looking from. When I apply the same transform interpolation the result will be our target object smoothly tracking our target object at a fixed distance.