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