I'm trying to make a player rotate to face in the direction of a virtual joystick. I don't want to use look at(), because I want to be able to adjust the turning speed of the player, not have it instantly look in the direction of the joystick (unless this is possible with look at()).
Because I'm checking if rotation is larger or smaller than target dir, whenever target dir wraps around (goes from PI to -PI or the other way around), the player starts turning in the wrong direction.
Is there some obvious way around this, or built-in function I'm not seeing?
I appreciate any insight into this.
Here is the relevant code:
target_dir = Joystick.output.angle()
target_dir = atan2(-Joystick.output.y, -Joystick.output.x) #opposite direction of Joystick in radians
#a temporary, imperfect workaround. This makes it jumpy, and also does not work if the angle changes too suddenly.
if rotation > 2.7 && target_dir < -2.7 :
rotation = -PI
if rotation < -2.7 && target_dir > 2.7 :
rotation = PI
#Set rotation direction
rot_dir = 0
if rotation > target_dir:
rot_dir -= 1
rot_dir += 1
#finally, apply rotation based on direction set.
rotation += rotation_speed * rot_dir * delta