This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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: https://moose-stache.itch.io/pixel-roguelite-asset-pack

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)
in Engine by (37 points)

1 Answer

+2 votes
Best answer

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

by (3,505 points)
selected by

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)

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

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

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.

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

Cool! glad to help

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 :)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.