# Playing an animation relative to current Transform in unity?

I have an animation created with the built-in dope sheet. It plays correctly but I would like it to be played relative to current object transformation. For example I have a palm tree that moves its leaves. If I rotate the palmtree and then start the animation leaves does not play animation relative to its new position/orientation. I know it is playing this way because of the animation being absolute. Is there any way to make it realtive to current transformation?.

The only way to do this is to make an empty gameobject and then put the object you wish to animate inside it, then putting an animator on the new parent.

For example, here you would put the tree and leaves in a parent object, and transform the parent object as needed while the animation plays relative to the parent.

• Thant worked. Just had to made the palmtree child of a parent and apply the transform in the parent. – Notbad Apr 9 '14 at 11:19

Another way to handle this, which works well if your objects don't move, is to save the position of the object before you start the animation.

Like so:

        startPosition = transform.localPosition;
animation.Play("testAnim");


Then during LateUpdate, you jus add that position on top of the position of your object, which effectively shifts it by it's original position. Since htis happens in LateUpdate it happens AFTER the animation offset was applied, maintaining the animation.

Like this:

void LateUpdate() {
transform.localPosition += startPosition;
}


Since this offset is static this only works with non-moving objects. Of course you could modify this startPosition to move the object but it get's a bit more roundabout then.

Hope this helps :)

You can also :

• On the animator of the object you want to animate check "Apply root Motion"

• in the inspector of the animation, click "Generate Root motion Curves"

Et voilà :)

• would you mind to expand a bit your answer a bit more? What is this Apply root motion used for? And what about the root motion curves? I have been reading the doc and I can't get it. Please it would be nice if you could use just plain language. – Notbad Nov 23 '15 at 0:39