Hello all.
I am instantiating a prefab that has a rotation of 330,180,0 like this:
Vector3 pos = new Vector3(Random.Range(-5,25), 0, 25 );
Instantiate(prefab, pos, Quaternion.identity);
Problem is, when I see the object in game, after it’s been instantiated it resets the rotation back to 0,0,0. The prefabs script does not touch the rotation and it doesn’t have a rigidbody.
I read in another question that Quaternion.Identity is supposed to keep the prefab’s rotation.
What am I doing wrong?
Thank you.
You are specifying Quaternion.identity to use for rotation. Quaternion.identity is 0,0,0 only.
Try this:
Vector3 pos = new Vector3(Random.Range(-5,25), 0, 25);
GameObject go = Instantiate (prefab) as GameObject;
go.transform.position = pos;
//or, go.transform.localPosition = pos;
When you set it to “Quaternion.identity”, you’re zeroing the rotation. I’m not sure if you can copy the rotation of a prefab, but you can set it after instantiating it. It’ll be something like this:
Vector3 pos = new Vector3(Random.Range(-5,25), 0, 25 );
GameObject newObject = (GameObject) Instantiate(prefab) as GameObject;
newObject.transform.position = pos;
newObject.transform.localEulerAngles = new Vector3(x,y,z);