Detect which way the player is facing in a 2D platformer

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

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 action_pressed(“ui_right/left”) and action_just_released(“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.


:bust_in_silhouette: Reply From: njamster

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:
    elif direction == Vector2.LEFT:

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

fx46 | 2020-04-12 18:05

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.

njamster | 2020-04-12 18:19

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

fx46 | 2020-04-12 18:21

It worked !
This simple thing was really upsetting me.

Osamu | 2020-04-13 02:17