I want to make my player face the character face the direction he’s walking in (left or right). I’ve tried many things, but he ends up just facing to the right.
extends CharacterBody2D
var attacking: bool = false
func _ready():
%AnimationPlayer.animation_finished.connect(_on_animation_finished)
func _physics_process(delta):
var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
if attacking:
velocity = Vector2.ZERO
else:
velocity = direction * 400
move_and_slide()
if not attacking:
if velocity.length() > 0.0:
%Bluwee.play_run()
else:
%Bluwee.play_idle()
if Input.is_action_just_pressed("attack") and not attacking:
attacking = true
%Bluwee.play_attack()
func _on_animation_finished(animation: String):
if animation == "side_attack":
attacking = false
%Bluwee.play_idle()
It would be nice to know what those “things” are so others could explain what went wrong.
It’s not clear to me how your character is moving. Can you move on both axes (like in your code), or do you only run left/right (as eluded to in your problem description)?
For platformer movement, you can use the flip_h property in Sprite2D to flip the visual representation of your character (see tutorial example).
For top-down directional movement, you would probably want to rotate your character (see Node2D’s look_at() method).
I am sadly not seeing anything in your code that could affect your character’s sprite. As of now, I am assuming you do it all magically inside your animation which is obscured by %Bluwee.play_XXX()
I also have no information about the SceneTree of your CharacterBody2D so I am not sure if you are using an AnimationSpriteFrame, a Sprite2D or something equivalent.
My headcanon is this:
Inside all your %Bluwee animations, you set your animations to be displayed with your character facing right without considering the flip_h property.
In your code, you don’t differentiate your velocity.x or direction.x to be either <0 or >0, which means your character doesn’t even know if he is currently walking to the left or right.
If he did know that, he could have set sprite2D.flip_h = true and everything would be fine.
How do you know when to flip the Sprite2D, how do you differentiate when to use left-facing and right-facing sprites?
I thought you need to see what kind of input you make via var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
If the player is walking right, then his direction.x must be higher than 0.
If the player is walking left, then his direction.x must be smaller than 0.
Your question puzzles me, so I assume you have some kind of other system to determine when to flip the sprites.
true lol, I’ve tried using $Sprite2D.flip_h = true. Now I’ve deleted everything related to flipping the player, since it doesn’t work.
after reading that, I would assume you are now somehow differentiating them inside of the %Bluwee animations themselves.
Since you are no longer flipping the sprite2ds and using a different method, you are free to disregard my statements about the direction.x and velocity.x.
Right now I would propose to set it to sprite.flip_h = true if direction.x < 0.0 else false
as I believe that pressing any of the ordinal directions (NW, NE, SW, SE) will result in a direction value of 0.5, 0.5
Before proposing it, I would need to know why it didnt work as Niku already tried to set the flip_h true and false. There might be an error in the code or maybe his %Bluwee animations overwrote the property.