0 votes

I have a character (2d kinematic body) which consists of several sprites, collisions and animation nodes, and other scenes may be added to it in the future. I need to reflect all this on the axis of height and back, depending on where the character is moving.

func move(speed: float) -> void:
    var velocity := Vector2.ZERO
    if Input.is_action_pressed("p1Up"):
        velocity.y = -1
    if Input.is_action_pressed("p1Left"):
        velocity.x = -1
    if Input.is_action_pressed("p1Down"):
        velocity.y = 1
    if Input.is_action_pressed("p1Right"):
        velocity.x = 1

    move_and_slide(velocity * speed)
Godot version 3.2.3.stable.official
in Engine by (154 points)

1 Answer

0 votes

Here's two possible ways to do it:

Potential solution 1
Find all of the Sprites and flip them:

# This will be true if the player's trying to move to the left.
# If the player's not trying to move to the left, then it will be
# false.
var flip_h = velocity.x < 0
for child in get_children():
    if child is Sprite:
        child.set_flip_h(flip_h)

Potential solution 2
This is probably the better way to do it. First, make sure that all of the Sprites that you want to flip are in a group named "to_flip". Then:

# This will be true if the player's trying to move to the left. If
# the player's not trying to move to the left, then it will be
# false.
var flip_h = velocity.x < 0
get_tree().call_group("to_flip", "set_flip_h", flip_h)
by (363 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.