/// <summary>
/// Lerp the current transform rotation to point towards world position "position" using t.
/// The look-at is planar, meaning that the transform.up will be Vector3.up.
/// </summary>
public static void LerpLookAtPlanar(Transform transform, Vector3 position, float t)
{
Vector3 targetDir3D = (transform.position - position);
if (targetDir3D.sqrMagnitude < 0.001f)
{
return;
}
Vector2 targetDir = new Vector2(targetDir3D.x, targetDir3D.z);
Vector2 currentDir = new Vector2(transform.forward.x, transform.forward.z);
targetDir.Normalize();
currentDir.Normalize();
var desiredAngle = MathHelper.AngleCounterClockwise(targetDir, Vector2.down) * Mathf.Rad2Deg;
var currentAngle = MathHelper.AngleCounterClockwise(currentDir, Vector2.up) * Mathf.Rad2Deg;
currentAngle = Mathf.LerpAngle(currentAngle, desiredAngle, t);
transform.rotation = Quaternion.AngleAxis(currentAngle, Vector3.up);
}