How to smoothly rotate object using aim_to function?

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

aim_to immediately sets needed rotation for object. It’s good for aiming player by mouse but aiming bots looks unnatural.

Is it possible to smoothly rotate object from previous position to new when you aim to new point?

:bust_in_silhouette: Reply From: dustin

you can use

lerp($cameranode.rotation.x, aim_to($player.x), 0.5 #adjust this)
lerp($cameranode.rotation.y, aim_to($player.y), 0.5 #adjust this)
lerp($cameranode.rotation.z, aim_to($player.z), 0.5 #adjust this)


:bust_in_silhouette: Reply From: Robotex

I assume this should work but I haven’t tested it yet.

export var angular_acceleration = 2

func lerp_angle(from, to, weight):
	return from + short_angle_dist(from, to) * weight

func short_angle_dist(from, to):
	var max_angle = PI * 2
	var difference = fmod(to - from, max_angle)
	return fmod(2 * difference, max_angle) - difference

func _physics_process(delta):
	var direction : Vector2 = target_position - global_position
	direction = direction.normalized()
	rotation = lerp_angle(rotation, direction.angle(), angular_acceleration * delta)