+3 votes

I occupy the character to turn vertically only according to the position of the mouse, so if the mouse is below the player it does not detect it, it only detects when it is to the right or to the left

I tried to make an animation according to the degrees of rotation and it went from a number of degrees to return to the stated amount but it gives many failures

in Engine by (52 points)

1 Answer

+2 votes
Best answer

If you want to flip a sprite and its pivot is centred you could use something like:

#Horizontal flip
if get_global_mouse_position().x > position.x && is_flipped_h() == false:
    set_flip_h(true)

elif get_global_mouse_position().x < position.x && is_flipped_h() == true:
    set_flip_h(false)

#Vertical flip
if get_global_mouse_position().y > position.y && is_flipped_v() == false:
    set_flip_v(true)

elif get_global_mouse_position().y < position.y && is_flipped_v() == true:
    set_flip_v(false)

In this example the character should be facing left if it's a profile sprite.

If you want to gradually rotate you sprite looking at your mouse you could use the look_at() function in combination with get_global_mouse_position(). If you want to use this move the pivot of your sprite to where the rotation should occur.

For example:
If I have the sprite of an arrow and I want it to point towards the mouse I have to move the pivot of the arrow sprite to the base of the arrow.

by (397 points)
selected by

Ready thanks bro, it helped me a lot

There is an error which says "The method "isflippedh" isn't declared in the current class"

Make sure you are using an sprite or an animatedsprite and that your script is extending one of these classes

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 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 webmaster@godotengine.org with your username.