1
\$\begingroup\$

I have been working on making a platformer in Godot but can't seem to get a walk cycle to play when I press left or right.

extends KinematicBody2D

export (int) var run_speed = 300
export (int) var jump_speed = -400
export (int) var gravity = 1000

var velocity = Vector2()
var jumping = false

func get_input():
    velocity.x = 0
    var right = Input.is_action_pressed('ui_right')
    var left = Input.is_action_pressed('ui_left')
    var jump = Input.is_action_just_pressed('ui_select')

    if jump and is_on_floor():
        jumping = true
        velocity.y = jump_speed
    if right:
        velocity.x += run_speed
    if left:
        velocity.x -= run_speed

    if left or right and jumping != false:
       get_node("AnimationPlayer").play('walkcyc')

    if left or right and jumping != true:
        $AnimatedSprite.animation = 'run'
        $AnimatedSprite.flip_h = velocity.x < 0

    if is_on_floor() and velocity.x == 0 or velocity.y == 0:
        $AnimatedSprite.animation = 'stand'
        $AnimatedSprite.flip_v = false
        $AnimatedSprite.flip_h = velocity.x < 0

    if jumping != false:
        $AnimatedSprite.animation = 'jump'
        $AnimatedSprite.flip_v = false
        $AnimatedSprite.flip_h = velocity.x < 0

func _physics_process(delta):
    get_input()
    velocity.y += gravity * delta
    if jumping and is_on_floor():
        jumping = false
    velocity = move_and_slide(velocity, Vector2(0, -1))
\$\endgroup\$
  • \$\begingroup\$ unless you know for certain that (a or b and not c) is equivalent to ((a or b) and not c) then really you should explicitly wrap parenthesis around (a or b) so in your if statements it should be "if (left or right) and jumping != false:" \$\endgroup\$ – Joshua Hedges Jan 17 at 23:55
  • \$\begingroup\$ Effectively this would be interpreted as (left or (right and jumping != true)) instead due to "and" usually having a higher precedence. \$\endgroup\$ – Joshua Hedges Jan 17 at 23:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.