This code snippet will rotate in the direction of the current mouse position with a limit on the rotation speed. This behavior is useful for gun turrets or other situations where unbound rotation speed is not appropriate. You can add a Sprite2D for a quad facing the X-positive direction to visualize the direction.

# turret.gd

@export var turretRotateSpeed = 2

func rotateTurret(delta):
    var targetAngle = (get_global_mouse_position() - global_position).normalized().angle()
    var deltaAngle = fmod((targetAngle - rotation + PI), (PI * 2)) - PI
    var maxRotation = turretRotateSpeed * delta
    var rotationChange = clamp(deltaAngle, -maxRotation, maxRotation)
    rotation += rotationChange
    # rotation = clamp(rotation, -3 * PI / 4, 3 * PI / 4) # optional angle limit