Ok I figured it out
I had to use the difference of the angles (as Lopy said) and the dot product
I'll put the code here in case anyone needs it:
var rotation_speed = deg2rad(45.0) # Since all values are in radians, this needs to be in radians too
func getDot(vector1: Vector2, _vector2:Vector2) -> float:
var n1 = vector1.normalized()
var n2 = _vector2.normalized()
var vDot = n1.dot(n2.tangent()) # Why the tangent? Because the dot product is 0.0 when the angle between 2 vectors is 90º, and I need it to give 0.0 when the angle is 0º
return vDot
func _process(delta):
var player_dir = get_parent().get_node("Player").global_position - global_position
var angle = polar2cartesian(1.0,rotation) # This var is to obtain a vector with the turret angle so I can obtain a dot product
var difference : float = player_dir.angle() - angle.angle()
dot = getDot(angle, player_dir)
if difference != 0:
if abs(difference) > 0.1:
$icon.rotation += sign(dot) * rotation_speed * delta # If the dot product is < 0 that would make the turret to rotate counter-clockwise and the other way around
Maybe this can be optimized, but it works
Thanks for the help!