Root Motion in 2D

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

I’ve opened an issue and was told to ask that question in a dedicated place. Please, read the following:

EDIT

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 AnimationPlayer to 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

  1. Design an animation that affects object position (there will be a position track)
  2. In a script when the game runs disable that track (so that animation player does not try to tweak position by its own).
  3. 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 ANIMATION_PROCESS_MANUAL mode
    3.3. Play the animation
  4. 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.
  5. When you have the velocity, call move_and_slide (or similar method that you want) of your KinematicBody2D.
  6. 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).
  7. 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?
:bust_in_silhouette: Reply From: flurick

I got nothing but the github issue is now tagged as a feature proposal

Well, in short: it was finally interpreted as a proposal to implement root motion feature for 2D=) and the question initially was about how to implement “root motion”-like animation in 2D with some probable workaround solutions already proposed.

Now I do know that this can be achieved without help from the core team, but it still seems to be a good built-in feature to have

This question is left for history and for guys that need to do what I needed.

smedelyan | 2019-03-31 08:57