flip 2d sprite based on mouse location

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

i want my sprite to flip based on the mouse cursor whether it was on the right or the left of the character, however, I cant find my mistake

similar to this: Pixel RogueLite Asset Pack by Moose Stache

func _flip():
var angle= (get_global_mouse_position() - $Movement.position).angle()
if angle>(3*PI)/2 and angle <PI/2:
	$Movement.set_flip_h(true)
if !(angle>(3*PI)/2) and !(angle <PI/2):
	$Movement.set_flip_h(false)
:bust_in_silhouette: Reply From: p7f

I think it would be easier just to look at the sign of the difference of x components:

func flip():
    var direction = sign(get_global_mouse_position().x - $Movement.global_position.x)
    if sign < 0:
        $Movement.set_flip_h(true)
    else:
        $Movement.set_flip_h(false)

Note i used global_position for $Movement

Amazing it works now for the sprite how can i also make it flip the gun ?
i modified it like so but it still wont flip the gun

func flip():
var direction = sign(get_global_mouse_position().x - $Movement.global_position.x)
if direction < 0:
	$Movement.set_flip_h(true)
	$gun.set_flip_h(true)
else:
	$Movement.set_flip_h(false)
	$gun.set_flip_h(false)

Rashid | 2020-07-16 12:33

have you set the gun as a child of the $Movement node?

p7f | 2020-07-16 12:53

both the $Movement and $gun are childs of $Player

Rashid | 2020-07-16 17:44

IDK why it doesn’t flip then. If you share me a minimal project reproducing the issue, i can take a look and try to fix that.

p7f | 2020-07-16 17:47

Really appreciate ! it was a minor mistake instead of flipping horizontally i flpped vertically! and not it works

Rashid | 2020-07-16 20:22

Cool! glad to help

p7f | 2020-07-16 21:05

Hello I’m very new to godot and tried using this for my game. What does the $ mean? Because I’ve tried does $Wizzo (Name of the node for my character) and $Sprite (My sprite) but it still doesn’t work. Any help is appreciated :slight_smile:

Phloop | 2021-04-17 14:51

Hey! The $ stands for an animated sprite he has! Movement is simply what he named it.

cornarian | 2021-07-03 14:27