Else if math issue

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

So I’m trying to make the character change the sprite according to where the mouse is, so the character can “rotate” and be more realistic.

But somehow it’s not working then the rot variable is positive. When it’s negative it works properly.

Is it a bug in the engine or something?
And how am I supposed to fix it?

Thanks!

Here is the code:

func bow(rot):		
	$Bow.look_at(get_global_mouse_position())
	if Input.is_action_pressed("mouse_left"):
		$Bow.play("Charge")
	else:
		$Bow.play("Idle")
	
	#Facing Directions
	#Right
	if (rot >= -45) or (rot <= -315) or (rot == 0) or (rot >= 315) or (rot <= 45 and rot >= 0):
		facing_direction = Vector2(1, 0)
	#Left
	elif (rot >= 135 and rot <= 225) or (rot >= -225 and rot <= -135):
		facing_direction = Vector2(-1, 0)
		print("it's working")
	#Down
	elif (rot < 315 and rot > 225) or (rot > -315 and rot < -225):
		facing_direction = Vector2(0, 1)
	#Up
	elif (rot < 135 and rot > 45) or (rot > -135 and rot < -45):
		facing_direction = Vector2(0, -1)
	
	animation()
	
	print(rot)
:bust_in_silhouette: Reply From: jgodfrey

Your problem looks like a logic bug to me. Here…

 #Facing Directions
    #Right
    if (rot >= -45) 

Any positive value of rot will always be greater than -45, so it’ll always process that block of code when the value is positive.

Additionally, you can probably simplify your logic by adding 360 to any rot value that’s less than 0.

jgodfrey | 2021-01-12 19:05

OMG!!! IT WORKED!!!

Thanks a LOT!!!

shoyguer | 2021-01-12 19:10