Unable to set position of instantiated prefab
I have been combing the forums and debugging this for a while and am hoping someone here would kindly help me on this.
I have Unity 2017.3.0f3 and am trying to instantiate an object, then set its position. Here's the code snippet.
if (dead)
{
rb2d.velocity = (new Vector2(0, 0));
GameObject deathAnim = Instantiate(deathAnimation, new Vector3(3,3,0), transform.rotation);
//deathAnim.transform.position = transform.position;
Debug.Log(deathAnim.name + " " + deathAnim.transform.position + " " + transform.position);
transform.position = startPosition;
if (carrying)
{
DropObject();
}
dead = false;
}
For some reason the clone always get's created at 0,0,0 and it won't move to the desired position I've tried:GameObject deathAnim = Instantiate(deathAnimation, new Vector3(3,3,0), transform.rotation);
As well as
GameObject deathAnim = Instantiate(deathAnimation); deathAnim.transform.position = transform.position;
When I Debug.log the position of deathAnim, it has the correct position in the console. But when I pause the game and click on the deathAnim clone, the position in the inspector reads 0,0,0. This really confuses me.
Pleas help, this is going to give me an aneurysm. (sorry for my terrible suck with markdown).
Isn't the AntDeatController script changing the position in awake or something? Or maybe setting it as a child of something?
...or maybe it's the Animator that changes its position?
The positioning code is correct, so it must be something else changing the position.
Answer by ouoertheo · Apr 21, 2018 at 02:41 PM
You're right! The animator seems to be setting absolute values instead of relative. This was because I was not using Apply Root Motion and then Generate Root Motion Curves on the animation from my assets. Found out from this article https://answers.unity.com/questions/234957/animation-editor-relative-animation-positions.html
Thanks for pushing me in the right direction @Harinezumi
Your answer
Follow this Question
Related Questions
Simple Prefab Instantiate "(Clone)" Question 3 Answers
destroy asset is not permitted to avoid data loss? 0 Answers
I need help destroying a clone on function. 1 Answer
Saving Instantiated objects,Saving Instantiated objects 0 Answers
Prefab Enemy2 to track all prefab Enemy1's Instantiates transforms: How? 1 Answer