How can I make a sprite flip horizontally after it reaches a certain amount of degrees?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HarryCourt
:warning: Old Version Published before Godot 3 was released.

So, my player is holding a weapon that points towards the mouse, but it looks crooked when it goes 180 degrees, so is there any way I can flip it once it reaches (-)90 degrees?

Script:

extends Sprite

#var gunRotation = rotation_degrees(0, 0)

func _process(delta):
	var mpos = get_global_mouse_position()
	look_at(mpos)

you can flip the sprite vertically once it passes ±90 degrees angle(might need to set it to different angle because godot’s angles are kinda weird) using get_rot() or rotation if you are on godot 3 branch and flipping accordingly

rustyStriker | 2017-12-21 18:31

:bust_in_silhouette: Reply From: avencherus

Yes, the angle is found in the vector between the cursor and the gun tip (or aim origin).

var ang = (get_global_mouse_position() - gun_tip.get_global_pos()).angle()

print(rad2deg(ang))

Just make sure your angles match Godot, or do a conversion on your angles.

0 points down, 180 and -180 point up.

From 0 heading clockwise will cover 0 to -PI (-180), and the counter-clockwise direction covers 0 to +PI (+180).

I’ve tried my best, but I get this result: Screen capture - 43a0d74d75707281ecc27a60f2411688 - Gyazo

Here’s the code;

extends Sprite

func _process(delta):
	var mpos = get_global_mouse_position()
	var ang = (get_global_mouse_position() - self.get_global_position()).angle()
	
	look_at(mpos)
	
	print(rad2deg(ang))
	
	if (ang >= 90):
		#elf.set_flip_h(false)
		self.set_flip_v(false)
	elif (ang >= -90):
		self.set_flip_h(true)
		self.set_flip_v(true)

HarryCourt | 2017-12-22 07:35