The question is answered, right answer was accepted
How to get perfect position animation with root motion?
I have a problem with a 3D sphere animation. Given a simple sinusoidal transformation in, say, x-position, one loop cycle ends always in the coordinate of origin. However, when using Root Motion, the loop ends always a little bit shifted from the origin.
Here is how to replicate the problem:
Create new project.
Create empty GameObject and a 3D Sphere child object to it.
Add Animator component to the Sphere
Sphere selected, create new Animation (this also creates Animator Controller)
Create sinusoidal loop animation (called "Oscillation")
Create Animator bool variable, say, "oscillate"
Create new Idle state (mark as default) and create transformation to "Oscillate" animation, add conditional transformation using the "oscillate" variable (true->transform to "Oscillate", vice versa)
Check "Apply Root Motion" in Sphere Animator component, create Root Motion Curves for the animation from the Inspector view of the animation
Setup
9.Lock the Sphere object inspector view, Play the scene and toggle the bool variable "oscillate" in the Animator view. Every time one loop round ends, the sphere is in another position.
Observed slight shift of the object
This behavior does not occur when Root Motion is not checked in the Animator component. The direct consequence of this behavior is that when switching between Idle and Oscillating several times, the Sphere moves from the position (0, 0, 0) to, say, (0.87, 0, 0).
What is going on and how to avoid this behavior? Is it a bug or is it completely normal (if yes, then, why?)?
Answer by juliuslaak · Mar 28, 2016 at 12:26 PM
I solved this problem bruteforce by attaching the animation state a script with the following functions:
private Transform transform;
private Vector3 transformPosition;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
transform = animator.GetComponent<Transform>();
transformPosition = transform.position;
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
transform.position = transformPosition;
}
Follow this Question
Related Questions
Player teleports when animation finishes 0 Answers
Root motion on animation clips created at runtime 0 Answers
Root Motion not having gravity (while Animate Physics turned on). 1 Answer
It is possible to create AnimationClip ingame whose do not loop / reset position? 0 Answers
How To preserve Forward momentum of an anim throughout another anim? 0 Answers