9
\$\begingroup\$

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?.

Thanks in advance.

\$\endgroup\$
1

3 Answers 3

15
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Thant worked. Just had to made the palmtree child of a parent and apply the transform in the parent. \$\endgroup\$
    – Notbad
    Commented Apr 9, 2014 at 11:19
8
\$\begingroup\$

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 :)

\$\endgroup\$
6
\$\begingroup\$

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à :)

\$\endgroup\$
3
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Notbad
    Commented Nov 23, 2015 at 0:39
  • \$\begingroup\$ While "Apply Root Motion" will first appears to do the trick, there are cases/instances where you can't use it. The issue that "might" appears" with Apply Root Motion is if your animation is not perfectly reverse looped. (By that, I mean that the animation does the opposite movement after the initial movement). This is because the motion is added to the "root" and so ends up being additive and not relative. This is even more present if you're using a soft transition in the animator setting. \$\endgroup\$ Commented Nov 7, 2019 at 0:00
  • \$\begingroup\$ By now it is enough to check "Apply root Motion" on the Animator component in the Inspector. Generating Root Motion Curves is no longer necessary. Unity does that by itself. \$\endgroup\$ Commented Feb 17, 2023 at 6:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .