Search code examples
c#animationunity-game-engineunityscript

Prefab instantiated but animation not occuring at given position


I am making a 2d endless runner game in unity.

In that i have obstacle which has a sprite within an empty game object and has an animation component.

I have made these obstacles as prefabs and want to instantiate them by code at different positions.

so i use

Instantiate(Resources.Load("myPrefabsName") , new Vector3(0, ypos, -5), Quaternion.identity);

ypos is a float which has the y coordinate stored.

now this piece of code works but problem is that the Prefabs instantiates at the position where the animation is done and performs its animation as well.

I mean when i made the animation, i made it as to move its position from (0,0,-5) to (6,0,-5).

which works fine but because i instantiate at a diff position (i.e (0, ypos, -5)) it is not present at (0, ypos, -5) but is present at (0,0,0) animating towards (6,0,0).

How can i get it animating from (0, ypos, -5) to (6, ypos, -5)?


Solution

  • Sounds like that your root object is animated too (includes position data). You normally could fix it adding it to another game object as a root, that wouldn't be animated.