- Home /
Specify rotation of instantiated object
Hey there,
How do I specify the rotation of an object I am instantiating?
I am using the following code:
Instantiate (gameObjectName, Vector3(xVal, yVal, zVal), Quaternion.identity);
But they always instantiate 'backwards', I have tried transform.rotation, gameObjectName.transform.rotation .etc and they all yield the same results.
Answer by rudolfwm · Feb 17, 2011 at 10:20 AM
They are 'backwards' because the Prefab you use is rotated that way.
Try to use localRotation:
transform.localRotation = Quaternion.identity;
Or fix the rotation in your Prefab
ah, but the prefabs are rotated the way I want them! I gave transform.localRotation a go but it gave the same backwards facing result. :(
i found a solution, gameObjectName.transform.rotation seems to be grabbing the prefab rotation correctly!
yes you need to use the transform of the instantiated object. You were probably using the transform of the object who was instantiating the object
Answer by TheDemiurge · Feb 17, 2011 at 10:24 AM
It might have to do with how you're exporting and importing the model. You want to make sure to freeze the transform on the object in your 3D package before exporting it, so that when the rotation is all 0's it's facing the direction you expect it to.
If the object you're instantiating is facing the correct way and its rotation is zeros, make sure it's not parented to an object that is rotated. This goes back to the above, where two wrongs can appear to make a right.
If all that is good so far, then gameObjectName.transform.rotation
should work. If you're not sure what's going on, then what you could do is keep a reference to the newly instantiated object and tell it to look at a certain direction you want it to face. For example:
var target: GameObject;
// set the target in the inspector
var newObj: GameObject = Instantiate (gameObjectName, Vector3(xVal, yVal, zVal), Quaternion.Identity);
newObj.transform.LookAt(target);
If after this, for example, your new object is 90 degrees left, then you should go back to your 3D software and rotate the object 90 degrees to the right, freeze the transform, then re-export/import.
Your answer
Follow this Question
Related Questions
How to rotate an instantiated object by a fixed amount before it is spawned 2 Answers
Upon being instantiated, why is my object moving upwards ? 2 Answers
Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers
Instantiated object rotation abruptly. 0 Answers
Instantiate question 1 Answer