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

3 Answers

3

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.

Ah great - I've tried that and it works - thanks

Thanks. This method works really well, even when I have to end up creating two empty gameobjects, ending up with MainObject--->EmptyChild--->EmptyChild--->ChildGraphics

For anyone reading this, Unity 5+ now includes functionality to do this without using an empty gameobject or needing to resort to script: https://gamedev.stackexchange.com/a/111368

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:

Chill Martin, sometimes it takes a little while for a moderator to get to the queue! But don't worry, it only takes 15 Karma to avoid it.

Hm. Sorry, I don't quite get it.

Thank you. This is better than using a parent gameobject.

This worked for me as still is a problem in 2020, I used a Trigger instead of the Play, works like a charm, thank you!

How I solved this problem: