I've opened an issue and was told to ask that question in a dedicated place. Please, read the following:
The approaches in the GitHub Issue are not precise enough. That can be easily seen with animation that moves the object 400 units forward and 400 units backward so that after the animation finishes the object has to stay right exactly where it was before.
Currently, it seems that I've finally get it working in the right way by switching
ANIMATION_PROCESS_MANUAL mode and manually controlling when to update the animation.
Let me know if you need an example of this final fixed approach.
In short the steps are
- Design an animation that affects object position (there will be a position track)
- In a script when the game runs disable that track (so that animation player does not try to tweak position by its own).
- When that animation is wanted to be played do the followig:
3.1. Adjust the key values of the position track so that the first key has value of the current actual position, the rest keys are (relatively to the adjusted first key) the same as they were (they need to be simply shifted so that the animation stays the same, just from the different start position).
3.2. Switch animation player into
3.3. Play the animation
- On each physics frame you will need to have calculated velocity to move from key1 to key2, key2 to key3 etc. You can pre-calculate and cache it if you want or leave it being calculated each frame for the sake of simplicity.
- When you have the velocity, call
move_and_slide (or similar method that you want) of your
- As the very last step call
AnimationPlayer.advance(delta). You need to call this one only in the end of each frame (otherwise you will lose 1 frame and your animation will be slightly inaccurate).
- When the animation is finished switch the animation player back to
ANIMATION_PROCESS_IDLE mode (or whatever it was in the beginning).
Please, do let me know if there are more simplistic solutions or corner cases when the solution above won't work
Known issues, limitations and/or stuff to enhance
- The velocity between key1 and key2 is constant. Although, it is feasible to implement kind of "cubic" (or whatever you want) velocity change
- It is not clear (and in fact depends on the game requirements) what to do if the animation is interrupted. Immediately stop moving? Continue moving by inertia? Something else?