x


Instantiated Object looses prefab's rotation

I've created an obect, say a capsule, and in the scene windown i've rotated it 90 degrees, and now its a bullet. However when I instantiate this object, the rotation i've aplied the object/prefab doesn't follow through, and it still instantiates in its original orientation...

Any ideas?

more ▼

asked Jul 21 '11 at 03:43 PM

BobbleHead gravatar image

BobbleHead
189 29 48 61

Did you rotate the object before making it a prefab? If not, try doing so.

Jul 21 '11 at 03:58 PM Chris D

just tried this, and again, no luck =/

Jul 21 '11 at 04:54 PM BobbleHead

I'm instantiating with this code:

    var tempHundred: Transform;
    tempHundred = Instantiate(hundred,transform.position, transform.rotation);

could that be the problem?

Jul 21 '11 at 05:00 PM BobbleHead

What is this attached to? I.E. Which transform is getting passed to it? Because that's what's going to determine it's position and rotation.

Jul 21 '11 at 05:11 PM Chris D
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Which rotation are you using in the Instantiate instruction? If it's Quaternion.identity, the bullet keeps the same rotation as the prefab. If it is transform.rotation, then the object to which you've attached the script probably is rotated too - its rotation is combined to the original prefab's, producing the wrong direction.
If that's the case, add an empty game object to your weapon as a child, align it to have the blue axis pointing the correct shoot direction, then move your script to this object - the bullet will be instantiated with the correct orientation.

more ▼

answered Jul 21 '11 at 05:10 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

The code is part of the enemyscript.js, this controls enemy lives etc. i have this little bit of code:

if(EnemyLives<=0) { Destroy(gameObject); var tempHundred: Transform; tempHundred = Instantiate(hundred,transform.position, transform.rotation); }

But that don't work, i tried :

    tempHundred = Instantiate (hundred, Vector3(0,2,-14), Quaternion.identity);

which didn't do the rotation either, neither did it spawn at the enemies location (but i guess that cause the code didn't tell it too!)

Jul 22 '11 at 01:02 PM BobbleHead
    var tempHundred: Transform;
    tempHundred = Instantiate(hundred, new Vector3(-1.48088, 2, -12.85), Quaternion.Euler(90, 0, 0));

The above code fixed it! (i did some learning). however trying to get it to spawn at enemy location.. will post another question! Thank you!

Jul 22 '11 at 02:09 PM BobbleHead

I've done it! haha

var tempHundred: Transform; tempHundred = Instantiate(hundred, transform.position, Quaternion.Euler(90, 0, 0));

I am happy now

Jul 22 '11 at 02:12 PM BobbleHead

It's the correct way: transform.position is the point at which you should instantiate the new object. You should also use transform.rotation instead of Quaternion.Euler(...) to keep the enemy orientation, but case this refuses to work you can keep the Quaternion.Euler thing. You will have problems only case the enemy dies in another orientation, because the new object will always be spawned facing the same side.

Jul 22 '11 at 04:38 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

maybe this is a little old, but I ran over the same issue and came up with two simple solutions:

1st- Place your object inside a empty game object at the desired rotation and create the prefab from this empty one. When you instantiate your prefab at identity rotation, the actual object inside the parent will keep the original saved rotation.

2nd- use this code to initialize object: Instantiate (myPrefab, new Vector3 (x ,y, z), myPrefab.transform.rotation); To be honest, I've not tried the second one, but I'm assuming it will spawn it at the prefab stored rotation.

good luck!

more ▼

answered Nov 29 '12 at 09:44 PM

GeorgeRigato gravatar image

GeorgeRigato
157 4 6 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2168
x1678
x1281
x1259
x1

asked: Jul 21 '11 at 03:43 PM

Seen: 4111 times

Last Updated: Nov 29 '12 at 09:44 PM