0 votes

Whenever I press my movement key it doesn't play the walking animation loop and stays on the idle animation. If anything it will only play the first frame.

Here is my movement script:

extends KinematicBody2D

var is_moving = false


var motion : = Vector2()

const MOVE_SPEED : = 150

func _physics_process(delta):

    #move script
    if Input.is_action_pressed("left"):
        motion.x = -MOVE_SPEED
        is_moving == true
    elif Input.is_action_pressed("right"):
        motion.x = MOVE_SPEED
        is_moving == true
    else:
        motion.x = 0
        is_moving == false

    #this makes the move script work bro
    motion = motion.normalized() * MOVE_SPEED
    motion = move_and_slide(motion)

#animation container... contains animations.
func _process(delta):

    if is_moving == false:
        $AnimatedSprite.play("idle")
    if is_moving == true:
        $AnimatedSprite.play("walking")

Any help is appreciated.

Godot version 3.2
in Engine by (21 points)

2 Answers

+1 vote
Best answer

Your problem is that when you set is_moving, you use two equal signs. That is a comparison, just use one when setting a value. The code below is the same as yours but I just changed the equals signs where you need them. Hopefully this helps, if not, just ask and I can try and help you!

extends KinematicBody2D

var is_moving = false


var motion : = Vector2()

const MOVE_SPEED : = 150

func _physics_process(delta):

    #move script
    if Input.is_action_pressed("left"):
        motion.x = -MOVE_SPEED
        is_moving = true
    elif Input.is_action_pressed("right"):
        motion.x = MOVE_SPEED
        is_moving = true
    else:
        motion.x = 0
        is_moving = false

    #this makes the move script work bro
    motion = motion.normalized() * MOVE_SPEED
    motion = move_and_slide(motion)

#animation container... contains animations.
func _process(delta):

    if is_moving == false:
        $AnimatedSprite.play("idle")
    if is_moving == true:
        $AnimatedSprite.play("walking")
by (459 points)
selected by

It worked! Thanks so much. I can't believe I've believed for so long that it meant it was setting it equal to true.

Awesome I’m glad it helped!

0 votes

have a fail in your code. when your press a key to move the "is_moving" var not change to true. because your still comparing using "==" to set the value you need to use a single equals sign.

Comparating is like

if is_moving == true:
    do_that!

Set a value is like:

is_moving = true

in your code need put that:

#move script
    if Input.is_action_pressed("left"):
        motion.x = -MOVE_SPEED
        is_moving = true
    elif Input.is_action_pressed("right"):
        motion.x = MOVE_SPEED
        is_moving = true
    else:
        motion.x = 0
        is_moving = false
by (32 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.