The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

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 (95 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 (810 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 Frequently asked questions and 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 [email protected] with your username.