(2D) Trying to make the player smoothly rotate to the mouse position but it rotates backwards when it becomes a negative number

Viewed 17

I am trying to rotate my top down player to the mouse position, I got it working but when it reaches the edge of the rotation degress it goes from a positive number into a negative number causing the rotation to go backwards instead of continuing the rotation, now I know why it happens but I cant figure out how to fix it. (reason to not use look_at is because I cant make it smooth to the mouse position.)

func apply_rotation(_delta):
    var mouse_direction = position - get_global_mouse_position() # Get the direction
    
    var correction_angle = 90 # make it so the player is facing correctly to the mouse
    
    var angle = rad2deg(-atan2(mouse_direction.x, mouse_direction.y)) - correction_angle # Angle to the mouse
    
    $Rotation.rotation_degrees = lerp($Rotation.rotation_degrees,angle,5 * _delta) # Lerp the angle smoothly
    
    $CollisionShape2D.rotation_degrees = $Rotation.rotation_degrees
0 Answers
Related