Rotating a RigidBody2D towards the player using set_applied_torque()

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

Hi, I want a RigidBody2D to rotate towards the player using set_applied_torque() in _integrate_forces(), but I can’t find the right formula to achieve that. My understanding is I have to find the direction (an angle in radians, I guess?) towards the player’s position and then rotate in one or the other direction until the RigidBody’s rotation matches.

If I’m correct, finding out that angle works via (player.global_position - global_position).angle() – but what do I compare it to to find out what torque to apply?

Okay, I came upon a solution to work with for now. Not perfect but does what it should.

var angle_to_player = rad2deg(get_angle_to(player.position))
if angle_to_player >= -90 and angle_to_player <= 0:
	set_applied_torque(get_parent().turning_speed)
else:
	set_applied_torque(-get_parent().turning_speed)

4ndrea | 2021-05-27 13:02