+1 vote

Hi,
I'm new to game development and everything coding and I'm trying to make a prototype of a 2d platformer.
I've created two animations for each basic action like idling, running or jumping, whether the player is facing left or right, due to the character carrying a sword in one hand (flip h is not viable).
So how do I detect which way the player is facing in order to trigger the "jump facing left" or "jump facing right" animation? For running and idling is easy, just use actionpressed("uiright/left") and actionjustreleased("ui_right/left") respectively. But for actions like jumping or attacking you just can't do it like that.
I'm using the node AnimatedSprite.

Thanks

asked Apr 12 in Engine by Osamu (16 points)

1 Answer

+2 votes
Best answer

Simply create a variable to store the direction the player is facing:

# assuming the character by default looks to the right
var direction = Vector2.RIGHT

func _physics_process(delta):
    # if the player presses both keys at the same time
    # the character will be looking to the right
    if Input.is_action_pressed("ui_left"):
        direction = Vector2.LEFT
    if Input.is_action_pressed("ui_right"):
        direction = Vector2.RIGHT

    if direction == Vector2.RIGHT:
        $AnimatedSprite.play("<NameOfAnimation1>")
    elif direction == Vector2.LEFT:
        $AnimatedSprite.play("<NameOfAnimation2>")
answered Apr 12 by njamster (8,904 points)
selected Apr 13 by Osamu

Isn't this the same solution as I commented before you?

answered 6 hours ago by njamster
answered 5 hours ago by fx46

Yes, it's the same solution. But no, you did not answer before me.

Ah! You're right, it must've not shown your solution then, my mistake!

It worked !
Thanks
This simple thing was really upsetting me.

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.