So, sprite follows the mouse cursor. I need at 90 degrees to make the sprite turn around. My script isn't working:

extends Sprite

func _process(delta):
	var vert = get_global_mouse_position()
	var ang = (get_global_mouse_position() - self.get_global_position()).angle()

	look_at(vert)

	print(rad2deg(ang))
	
	if (ang >= 90):
		self.set_flip_h(false)
		self.set_flip_v(false)
	if (ang <= 90):
		self.set_flip_h(false)
		self.set_flip_v(false)

I don't fully understand what you mean by "I need at 90 degrees to make the sprite turn around." but I think I might know what the problem is. If you want to actually flip the sprite when you call set_flip_h or set_flip_v you need to pass true. Passing false will disable the sprite from flipping. So assuming you mean that if the angle is greater than 90 you want to flip the sprite, set flip_h and flip_v to true in the body of your first if statement.

Some code style things: you don't need parenthesis around the expression in your if statement. Also, you don't need to check if the angle is less than 90 since you already checked that it's greater than 90 and you can just use an else statement:

if ang >= 90: self.set_flip_h(true) self.set_flip_v(true) else: self.set_flip_h(false) self.set_flip_v(false)

@anomalocaris said: I don't fully understand what you mean by "I need at 90 degrees to make the sprite turn around." but I think I might know what the problem is. If you want to actually flip the sprite when you call set_flip_h or set_flip_v you need to pass true. Passing false will disable the sprite from flipping. So assuming you mean that if the angle is greater than 90 you want to flip the sprite, set flip_h and flip_v to true in the body of your first if statement.

Some code style things: you don't need parenthesis around the expression in your if statement. Also, you don't need to check if the angle is less than 90 since you already checked that it's greater than 90 and you can just use an else statement:

if ang >= 90: self.set_flip_h(true) self.set_flip_v(true) else: self.set_flip_h(false) self.set_flip_v(false) Thanks for comment Example: https://youtube.com/watch?v=eofB2Z4-00w I mean - when gun pass axis Y, character horizontally flips (Sorry, I'm still learning English)

That example involves at least three sprites that I can see: the player's head and arm are two sprites that always look at the mouse, and the player's body does not look at anything but flips when the mouse is to the left of the player.

You can probably do this without checking the angle; just check if the mouse coordinate is to the left of the player, assuming the player sprite faces right by default. If you were to code it this way it might look something like this (I did not run this code):

extends Sprite

func _process(delta):
    var vert = get_global_mouse_position()
    var gpos = self.get_global_position()

    if gpos.x < vert.x:
        self.set_flip_h(true)
        vert.x = -vert.x # our sprite would be facing away from the mouse after flipping
    else:
        self.set_flip_h(false)
    look_at(vert)

Note that to get it to look like that video you'll have to have three sprites in your player scene. The arm and head should be rotated and flipped; you'll have to either add or subtract 180 degrees when the sprite is flipped. The body should not be rotated, just flipped. The code above works with one sprite like the head or arm. I don't know if I got the rotation handled correctly.

EDIT: fixed an issue with the y coordinate direction getting inverted

@anomalocaris said: That example involves at least three sprites that I can see: the player's head and arm are two sprites that always look at the mouse, and the player's body does not look at anything but flips when the mouse is to the left of the player.

You can probably do this without checking the angle; just check if the mouse coordinate is to the left of the player, assuming the player sprite faces right by default. If you were to code it this way it might look something like this (I did not run this code):

extends Sprite

func _process(delta):
    var vert = get_global_mouse_position()
    var gpos = self.get_global_position()

    if gpos.x < vert.x:
        self.set_flip_h(true)
        vert.x = -vert.x # our sprite would be facing away from the mouse after flipping
    else:
        self.set_flip_h(false)
    look_at(vert)

Note that to get it to look like that video you'll have to have three sprites in your player scene. The arm and head should be rotated and flipped; you'll have to either add or subtract 180 degrees when the sprite is flipped. The body should not be rotated, just flipped. The code above works with one sprite like the head or arm. I don't know if I got the rotation handled correctly.

EDIT: fixed an issue with the y coordinate direction getting inverted

Thanks man, now I see.

4 years later