0 votes

I want to know how to make my 2D character (PLAYER) flip to left , right , up and down depends on the mouse cursor .

Example :

If I move my mouse cursor to left , my character sprite should flip to the left .
if I move my mouse cursor to right , my character sprite should flip to right .
and again if i move to up , it should play a new sprite(I will make a new sprite that lookin up top) .Also for down.

Please help me !!
I am new to godot .
***English is not my language so i might have some grammar or spelling mistakes . ***

in Engine by (12 points)

1 Answer

0 votes

Assuming that you're using an animated sprite attach this GDscript

extends AnimatedSprite

func _input(event):
    if event is InputEventMouseMotion:
        if event.relative.x > 0:
            $AnimatedSprite.animation = "right"
            $AnimatedSprite.flip_h = false
        elif event.relative.x < 0:
            $AnimatedSprite.animation = "right"
            $AnimatedSprite.flip_h = true
        elif event.relative.y < 0:
            $AnimatedSprite.animation = "up"
            $AnimatedSprite.flip_v = false
        elif event.relative.y > 0:
            $AnimatedSprite.animation = "up"
            $AnimatedSprite.flip_v = true

func _process(delta):
    $AnimateSprite.play()

And for a regular sprite

extends Sprite

func _input(event):
    if event is InputEventMouseMotion:
        if event.relative.x > 0:
            $Sprite.flip_h = false
        elif event.relative.x < 0:
            Sprite.flip_h = true
        elif event.relative.y < 0:
            $Sprite.flip_v = false
        elif event.relative.y > 0:
            $Sprite.flip_v = true
by (2,424 points)
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.

Categories