0 votes

hello,
I'm new and I currently set up my character to flip left and right by using "left" and "right" keys respectively.
currently, if I hold "left" and press "right" my character does not flip (which is exactly what I want).
but, while holding down "right", if I press "left" my character flips.
how do I make it so that pressing two keys at once cancels flipping?

export (int) var speed = 480

var velocity = Vector2()

func get_input(): # movement and flip
    velocity = Vector2()
    if Input.is_action_pressed("right"):
        velocity.x += 1
        $Sprite.flip_h = true
    if Input.is_action_pressed("left"):
        velocity.x -= 1
        $Sprite.flip_h = false
    if Input.is_action_pressed("down"):
        velocity.y += 1
    if Input.is_action_pressed("up"):
        velocity.y -= 1
    velocity = velocity.normalized() * speed
Godot version v3.3.2.stable.official
in Engine by (36 points)

1 Answer

+1 vote
Best answer

You can check for left and right simultaneously which will cancel the movement and flipping

export (int) var speed = 480

var velocity = Vector2()

func get_input(): # movement and flip
    velocity = Vector2()
    if not (Input.is_action_pressed("right") and Input.is_action_pressed("left")):
        if Input.is_action_pressed("right"):
            velocity.x += 1
            $Sprite.flip_h = true
        if Input.is_action_pressed("left"):
            velocity.x -= 1
            $Sprite.flip_h = false

    if Input.is_action_pressed("down"):
        velocity.y += 1
    if Input.is_action_pressed("up"):
        velocity.y -= 1
    velocity = velocity.normalized() * speed
by (719 points)
selected by
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.