- Home /
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
Answer by Adamcbrz · Apr 08, 2012 at 02:07 AM
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.
Thanks. This method works really well, even when I have to end up creating two empty gameobjects, ending up with $$anonymous$$ainObject--->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:
Answer by Democide · Jun 26, 2014 at 03:19 PM
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 :)
Chill $$anonymous$$artin, sometimes it takes a little while for a moderator to get to the queue! But don't worry, it only takes 15 $$anonymous$$arma to avoid it.
This worked for me as still is a problem in 2020, I used a Trigger ins$$anonymous$$d of the Play, works like a charm, thank you!
Answer by Knskan3 · Sep 04, 2014 at 07:40 PM
How I solved this problem:
http://answers.unity3d.com/questions/784567/how-to-make-animation-position-start-from-last-pos.html
Your answer
Follow this Question
Related Questions
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
Can I make animations snap to a frame? 1 Answer
How to select an animation clip by index number? 7 Answers
Will caching strings improve performance for animation? (and other string calls) 1 Answer