Smoothly cancelling and reversing an animation

Godot Version 4.2

Smoothly cancelling and reversing an animation

So there’s a lever which the player has to push, triggering the lever_on animation (rotation.x from 0 to 90), and when he lets go it goes back in the original position with the lever_off animation (rotation.x from 90 to 0).

If the player pushes all the way such that the lever action is activated the animations run fully and all is fine.

But if the player lets go too early, only part of the lever_on animation has played, so when starting to play the lever_off animation it jumps to the on position that is the beginning of that animation.

Instead, it should continue from the partial position it has reached and go back.

  1. can one stop a running animation and ask to go backwards from the reached time?
  2. more generally: can I determine the timestamp an animation has already played (and its total time) and start another animation from an offset?

use tween,
kill the running tween (this mean you have to make the rotating tween variable accessible outside, also always check first if the rotating tween variable existed first)
calculate remaining time to hit full left or right
use it to set the time to reach it by creating new tween on the same rotating tween variable, either using tween_property or tween_method