Animation Editor - 'Relative' animation positions

Hi there, I’m using the animation editor in Unity to create a quick prototype. All is well, except I need to translate the object that is animated in code. When I do this,and play the animation I recorded, it reverts back to the absolute position set in the animation editor (so for example if the first keyframe in the animation is at Y position 0, but I translate the object to 100, when I play the animation it snaps the object back to 0).

There must be a way around this I’m missing - can anyone help?
Thanks

Yeah this is an annoying aspect of unity animations. The easiest method is to create a parent gameobject that you position. Then the animation will play relative to that gameobject.

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

How I solved this problem: