Walk animation doesn't play when walking.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By FranticButter

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.

:bust_in_silhouette: Reply From: BVictor

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
:bust_in_silhouette: Reply From: djmick

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")

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.

FranticButter | 2021-02-06 21:54

Awesome I’m glad it helped!

djmick | 2021-02-06 22:13