I am having a tough time trying to get animations to work with 2D characters in Unity 4.3. I have a character made of parented Sprites (it doesn’t have a skeleton or any bones) and I want to change his position through animations created in Unity. However, to my knowledge, you can’t do root motion with Unity animations. My current solution to this is only moving the child of the root through animation, then saving the child’s position, setting the child’s localPosition to Vector3.zero, and finally moving the root to the saved child’s position. Unfortunately, when I do this, I can see the objects snapping into place and obviously I can’t have that.
My code for this was:
void Snap()
{
var savedPosition = Child.position;
Child.localPosition = Vector3.zero;
Root.position = savedPosition; // getting called first
}
After some testing I discovered that the last line was getting called before the one above it, which I thought was weird. So I wrote this:
IEnumerator Snap()
{
var savedPosition = Child.position;
Child.localPosition = Vector3.zero;
yield return new WaitForEndOfFrame();
Root.position = savedPosition;
}
This actually got rid of the flicker that happened with the other method. So, I guess my questions are:
Shouldn’t the line above it be called first no matter what? The root was moved first, then the child’s local position was reset to zero in both methods.
Why does waiting til the end of frame get rid of the flicker?
Is there a better way to do this?
Sorry for the long winded post. Thanks in advance.