Manipulating Physics in the Animation Player?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GdSat

In my 3d game, I have a concept of a cutscene which the player hits the ball (a rigid body node) and it moves along an arch. This is one of my first times using the Animation Player node, so I don’t know if there’s a way of taking advantage of the rigid body’s physics by using an animation.

Plus, since this is a cutscene, the ball needs to land in a spot as to trigger a dialogue.

Is there a way of doing that? Or do I need to this through code? How could I go about that

:bust_in_silhouette: Reply From: VitusVeit

Well since you already know where the ball needs to land I suggest you don’t use a RigidBody.
This is because (in my opinion) Godot’s physics and animation player don’t go well together.

I suggest that when you want to start the cutscene you swap the rigid body ball for a kinematic one, play the animation, and then swap it again, like this:

# Note: This isn't the best way to change the rigid body's mode, you could hide the static body.
# Then show another node used exclusively for the animation and then delete it when you're done, but this should work
func _start_cutscene() -> void:
    yield(get_tree(), "physics_frame")
    $RigidBody.mode = MODE_KINEMATIC
    yield($CutsceneAnimationPlayer, " animation_finished") # Wait for the cutscene to finish
    yield(get_tree(), "physics_frame")
    $RigidBody.mode = MODE_RIGID

There is also a way to update the animation player along the physics engine (playback process mode to physics). So you could, theoretically, apply a torque in the animation player but I never tried doing that.

EDIT: If you want to follow the example you can use the Bézier curves in the animation player to animate the ball and you need to wait for the physics frame before changing how a physics object behaves (this includes the position that should be changed only when the physics body’s mode is set to kinematic). It is a lot of stuff to learn but when you get the hang of it you can do amazing animations or games involving physics objects.