Animation not reversing as expected

Godot Version

4.3

Question

Hi, so i have been trying to make a script to play a certain animation “RESET” , which just moves a platformAnimatableBody2D in vertical direction, defined over 2 seconds. The script triggers the animation when the characterCharacterBody2D steps on the platform and as soon as it steps off, it plays back the same animation in reverse speed to it’s original position.

Now, everything works as intended, except when the animation reaches it’s end and as soon as the character steps off the platform, instead of playing back in reverse, it teleports to it’s original location, i.e., start of the animation RESET. I have tried implementing multiple fixes but nothing seems to improve the situation.

The script in question :

extends AnimatableBody2D

@onready var animation_player: AnimationPlayer = $AnimationPlayer
var top=false

func _physics_process(_delta: float) -> void:
	
	var space_state=get_world_2d().direct_space_state
	var query=PhysicsShapeQueryParameters2D.new()
	query.transform = Transform2D(0, global_position + Vector2(0, -10))
	query.shape = RectangleShape2D.new()
	query.shape.extents = Vector2(15, 5)
	query.collide_with_bodies=true
	var result=space_state.intersect_shape(query)
	var found_character = false
	
	for res in result:
		if res.collider is CharacterBody2D:
			found_character=true
			break
	
	if found_character and !top:
		animation_player.speed_scale=1
		animation_player.play("RESET")
		top=true
		
	elif !found_character and top:
		animation_player.speed_scale=-1
		animation_player.play("RESET")
		top=false

“RESET” is a special animation that is run when saving the scene, do not place actual animations on it, it is only used to reset the state of animated objects. That alone might fix the issue, otherwise maybe your animation loop is set up wrong

I tried renaming the script, nothing changed. Although about the animation loop, i am fairly certain it’s setup correctly. But then again, if i am wrong, i don’t know what is wrong.