Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity - animation moves game object away from where it is spawned?

Ok, this is frustrating but Ive created an animation loop on a prefab however after the prefab is spawned the animation (which is changes in position/rotation) runs the animation (thus moving the prefab) far away from where the prefab is spawned.

For example if my prefab is spawned at the origin, because of where I placed the prefab when I animated it, it animates in THAT place.

I need my prefab to run the animation but in the PLACE where it was spawned, not where I animated the original prefab. How can I fix this? What am I doing wrong?

I animated using Unity's animator.

like image 799
blue Avatar asked Oct 29 '22 17:10

blue


2 Answers

This is an common mistake in unity animation.

It's the case when you have to animate the position and at the same time change the position from the code.

In this case the simplest workaround would be to move your visuals to a child game object and animate the position of that object. Now you can change the position of the (parent) game object from code and still have the additive-like animation on the (child) game object.

like image 117
Bizhan Avatar answered Jan 02 '23 19:01

Bizhan


Uncheck "Apply Root Motion" in the animator. This will prevent the animator from moving the object. The object will then move using only your code.

like image 28
Tyler S. Loeper Avatar answered Jan 02 '23 19:01

Tyler S. Loeper