+1 vote

I created a walk script. Whenever my character is walking and i try to change direction, the character walks diagonally, before i release the button. How would I stop this from happening?

Edit:

#back
    if Input.is_action_pressed("Back"):
        velocity.y = -speed
        #$PlayerSprite.flip_h = false
        $icon.play("GoldWBack")
    elif Input.is_action_just_released("Back"):
        $icon.play("IdleBack")
        var facing_back = true
        if facing_back == true:
            if Input.is_action_just_pressed("LoadAmmo"):
                $icon.play("LoadArrowB")
            if Input.is_action_just_pressed("SwordSwing"):
                $icon.play("GSwordB")


    #forward
    elif Input.is_action_pressed("Forward"):
        velocity.y = speed
        $icon.play("GoldWForward")
    elif Input.is_action_just_released("Forward"):
        $icon.play("IdleForward")
        var facing_forward = true
        if facing_forward == true:
            if Input.is_action_just_pressed("LoadAmmo"):
                $icon.play("LoadArrowF")

    #left
    elif Input.is_action_pressed("Left"):
        velocity.x = -speed
        $icon.play("GoldWLeft")
    elif Input.is_action_just_released("Left"):
        $icon.play("IdleLeft")
        var facing_left = true
        if facing_left == true:
            if Input.is_action_just_pressed("LoadAmmo"):
                $icon.play("LoadArrowL")


    #right
    elif Input.is_action_pressed("Right"):
        velocity.x = +speed
        $icon.play("GoldWRight")
    elif Input.is_action_just_released("Right"):
        $icon.play("IdleRight")
        var facing_right = true
        if facing_right == true:
            if Input.is_action_just_pressed("LoadAmmo"):
                $icon.play("LoadArrowR")

SOLVED!!!

Basically, if you are only using 4 directions in your game then you should have this script layout (especially the _process). I recommend creating your own inputs.

func get_input():
    velocity = Vector2()
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    elif Input.is_action_pressed("ui_left"):
        velocity.x -= 1
    elif Input.is_action_pressed("ui_down"):
        velocity.y += 1
    elif Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    movement = velocity.normalized() * speed

func _process(delta):
    get_input()
    move_and_slide(movement)
in Engine by (304 points)
edited by

1 Answer

+1 vote
Best answer

Just a guess but maybe check for button up with not event.pressed so it does not happen 'before i release the button`

func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton and not event.pressed:
        print("ok")

Otherwise please provide more information or some example code please? You character only walks when button down? How does the turning work?

by (1,879 points)
selected by

Added script

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.