Sprite/object movement not consistent

Godot Version

4.3

Question

Hello, I’m new to Godot and game programming in general.
I’m making a simple 2D game and I’m having a “bug” to animate a projectile from the game.

I have a Player that shoots a Projectile upwards. The projectile should move upwards for a few moment, then it should stop in Y position while the sprite is animated until its finished.

I have the following script on the projectile:

extends RigidBody2D

@export var lifespan = 0

var posY = 0

func _physics_process(delta):
	linear_velocity.y = -4500 * delta
	posY = global_position.y
	await get_tree().create_timer(lifespan).timeout
	linear_velocity.y = 0
	global_position.y = posY
	get_node("AnimatedSprite2D").play("falling")
	await get_tree().create_timer(1).timeout
	queue_free()

The problem is the behavior of the projectile its not consistent all the time, sometimes it stops at Y position, and other times it continues to move upwards a little bit.

Any ideas on how to fix the issue?

Thanks.

1.Completely freeze the body to halt physics calculations

# linear_velocity.y = 0
freeze = true  # Freezes all physics simulations

2.To ensure an animation finishes playing in Godot, you can use the ​animation_finished signal of the AnimatedSprite2D node.

var animated_sprite = get_node("AnimatedSprite2D")
    animated_sprite.play("falling")
    await animated_sprite.animation_finished
1 Like

Thank you for the reply.

I changed the code on the second suggestion.

But I was using physics only to animate the sprite. I decided to change the node RigidBody2D to an Area2D and with the help of AI I’m using an algorithm to animate the sprite based on velocity and deceleration. Using Vector2.move_toward method.