Code for 2.5d sprite animation that worked in godot 3 no longer works in godot 4 and it make me sad :(

i am using godot version 4.3 and this code was made for version 3, ive tweaked it in every way i could possibly think of and it just wont work

heres the old code that originally worked in version 3:

extends CharacterBody3D

@export var speed = 1.7
@onready var anim = $AnimationPlayer
@onready var animationTree = $AnimationTree
@onready var animationState = animationTree.get("parameters/playback")

func Movement():
	if Input.is_action_pressed("ui_up"):
		velocity.z = -speed
		anim.play("bfwalk")
	elif Input.is_action_pressed("ui_down"):
		velocity.z = speed
		anim.play("ffwalk")
	else:
		velocity.z = lerp(velocity.z, 0, 0.5)
	if Input.is_action_pressed("ui_left"):
		velocity.x = -speed
		anim.play("lfwalk")
	elif Input.is_action_pressed("ui_right"):
		velocity.x = speed
		anim.play("rfwalk")
	elif Input.is_action_just_released("ui_left"):
		anim.play("lfidle")
	elif Input.is_action_just_released("ui_right"):
		anim.play("rfidle")
	elif Input.is_action_just_released("ui_up"):
		anim.play("bfidle")
	elif Input.is_action_just_released("ui_down"):
		anim.play("ffidle")
	else:
		velocity.x = lerp(velocity.x, 0, 0.5)

func _physics_process(delta):
	Movement()
	set_velocity(velocity)
	move_and_slide()

and the code as i currently have it [that doesnt work] in version 4.3:

extends CharacterBody3D


const SPEED = 1.7
const JUMP_VELOCITY = 4.5

var input_dir = Input.get_vector("move_left", "move_right", "move_up", "move_down")

@onready var anim = $AnimationPlayer
@onready var animationTree = $AnimationTree
@onready var animationState = animationTree.get("parameters/playback")

func Movement():
	if Input.is_action_pressed("move_up"):
		anim.play("bfwalk")
	elif Input.is_action_pressed("move_down"):
		anim.play("ffwalk")
	if Input.is_action_pressed("move_left"):
		anim.play("lfwalk")
	elif Input.is_action_pressed("move_right"):
		anim.play("rfwalk")
	elif Input.is_action_just_released("move_left"):
		anim.play("lfidle")
	elif Input.is_action_just_released("move_right"):
		anim.play("rfidle")
	elif Input.is_action_just_released("move_up"):
		anim.play("bfidle")
	elif Input.is_action_just_released("move_down"):
		anim.play("ffidle")


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("move_left", "move_right", "move_up", "move_down")
	var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()
	

the movement itself works perfectly fine its literally just the sprite animations that dont work

You’re not calling Movement()?

I would also suggest calling Movement() from _process, which is called once per frame, rather than _physics_process, which is called multiple times per frame

I would suggest to search all of the methods, involved in your code, on Godot docs and see if they have been modified or deprecated. Some may have changed the arguments that they expect or maybe also the way that they are called.

I had to change a bunch of code from 4.2 to 4.3 because the Tile Map will eventually be deprecated. How I’m calling the methods and the number of arguments changed.

This is called migration. It happens as frameworks/libraries/languages evolve. It is normal, is part of the game.

they’re simply not calling the function that plays the animation

wow i installed an entire mod trying to figure out how to get this to work and the issue was that i just simply forgot to call movement. can you tell this is my first time ever working in godot seriously thank you so much

1 Like

no problem. hot tip. if your code isn’t working, put a breakpoint at the start of it, run the game, then step through it line by line (f10) until you get to the end. usually you’ll notice what’s gone wrong just doing that.