This rotation idea of mine isnt working

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

I am trying set sprite based on direction of node facing an enemy.

#face nearest marble
		#  >>>>>  self.look_at(nearest_marble.position)- I changed to rotate for smoothness of movement
		var velocity = global_position.direction_to(nearest_marble.global_position)
		rotation = velocity.angle()
		print (rotation_degrees)
		if rotation_degrees <= 22.5 and rotation_degrees  >= -22.5: 
			$body/CollisionShape2D/shooter_sprite_button.set_normal_texture(right)
		if rotation_degrees <= 67.5 and rotation_degrees  >= 22.5: 
			$body/CollisionShape2D/shooter_sprite_button.set_normal_texture(bott_right)
		if rotation_degrees <= 112.5 and rotation_degrees  >= 67.5: 
			$body/CollisionShape2D/shooter_sprite_button.set_normal_texture(bott)
		if rotation_degrees <= 157.5 and rotation_degrees  >= 112.5: 
			$body/CollisionShape2D/shooter_sprite_button.set_normal_texture(bott_left)
		if rotation_degrees <= -22.5 and rotation_degrees  >= -67.5: 
			$body/CollisionShape2D/shooter_sprite_button.set_normal_texture(top_right)
		if rotation_degrees <= -67.5 and rotation_degrees  >= -112.5: 
			$body/CollisionShape2D/shooter_sprite_button.set_normal_texture(top)
		if rotation_degrees <= -112.5 and rotation_degrees  >= -157.5: 
			$body/CollisionShape2D/shooter_sprite_button.set_normal_texture(top_left)
		if rotation_degrees <= -157.5 and rotation_degrees  >= 157.5: 
			$body/CollisionShape2D/shooter_sprite_button.set_normal_texture(left)

The only one that works right is for right. Maybe its my dyslexia but I cant see my mistake.

Or if there is a better way to do this?

i cant see a reason why right is working and the other not, are you sure you are not simply seeing a default right texture and actually the entire code is not working at all?
add some print() below the if condition to check if it enters there.

also, maybe change all the if into if/elif/else: it does not make sense to let the computer check for each condition if you know it will already match only 1 of them

Andrea | 2021-01-18 22:30

I figured out what was wrong:

one I fixed the if to elifs.

Second I was rotating the texture_button! Which was incorrectly rotating the sprites. SO I added a child sprite, moved the rotation to child sprite, then added my muzzle spawn point to that child sprite and Viola I have my sprites functioning properly.

jbskaggs | 2021-01-19 01:44