Left and right walking animations not playing

Godot 4.1

I have been trying to make a character movement in Godot 4.1 and when I move the player up or down, the walking animations play, however when I move left and right, only the idle animations play?
(I’m using a sprite3D in a 3D scene, I don’t know if this is important or not.)
Here is my code in gdscript:

extends CharacterBody3D

const SPEED = 2
var facing = "Front"
var walking = 0

# Called when the node enters the scene tree for the first time.
func _ready():
	pass

# Movement
func _physics_process(delta):
	if Input.is_action_pressed("right") and Input.is_action_pressed("left"):
		velocity.x = 0
		walking = 0
	elif Input.is_action_pressed("right"):
		velocity.x = SPEED
		facing = "Right"
		walking = 1
	elif Input.is_action_pressed("left"):
		velocity.x = -SPEED
		facing = "Left"
		walking = 1
	else:
		velocity.x = 0
		walking = 0
	
	if Input.is_action_pressed("up") and Input.is_action_pressed("down"):
		velocity.z = 0
		walking = 0
	elif Input.is_action_pressed("up"):
		velocity.z = -SPEED
		facing = "Back"
		walking = 1
	elif Input.is_action_pressed("down"):
		velocity.z = SPEED
		facing = "Front"
		walking = 1
	else:
		velocity.z = 0
		walking = 0
	move_and_slide()

# Animations
func _process(delta):
	if walking == 1:
		$AnimationPlayer.play("Walking" + facing)
	else:
		$AnimationPlayer.play("Idle" + facing)

I have checked multiple times, and the animations are named correctly.

I am guessing walking turns to 0 on the second group of if elses. You should move and initialize walking to the top of all ifs.

I apologize, I am still very new to Godot, what do you mean by the second group of elses?

you can make 2 variables for the walking, walking_z, and walking_x
and check for both in the _process

extends CharacterBody3D

const SPEED = 2
var facing = "Front"
var walking_x = 0
var walking_z = 0

# Called when the node enters the scene tree for the first time.
func _ready():
	pass

# Movement
func _physics_process(delta):
	if Input.is_action_pressed("right") and Input.is_action_pressed("left"):
		velocity.x = 0
		walking_x = 0
	elif Input.is_action_pressed("right"):
		velocity.x = SPEED
		facing = "Right"
		walking_x = 1
	elif Input.is_action_pressed("left"):
		velocity.x = -SPEED
		facing = "Left"
		walking_x = 1
	else:
		velocity.x = 0
		walking_x = 0
	
	if Input.is_action_pressed("up") and Input.is_action_pressed("down"):
		velocity.z = 0
		walking_z = 0
	elif Input.is_action_pressed("up"):
		velocity.z = -SPEED
		facing = "Back"
		walking_z = 1
	elif Input.is_action_pressed("down"):
		velocity.z = SPEED
		facing = "Front"
		walking_z = 1
	else:
		velocity.z = 0
		walking_z = 0
	move_and_slide()

# Animations
func _process(delta):
	if walking_x == 1 or walking_z == 1 :
		$AnimationPlayer.play("Walking" + facing)
	else:
		$AnimationPlayer.play("Idle" + facing)

Thank you! This worked perfectly!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.