- Home /
How to transform rotation of a vector 3?
Hi, im trying to instantiate an object on a variable position. It works great, but i need the object to be on a variable rotation too, and i cant figure out how to do it. Any suggestion?
var thePositionToMoveTo : Vector3;
var prefab : Transform;
function OnTriggerEnter (col : Collider) {
if(col.name == "Player")
for (var i : int = 0;i < 1; i++) {
Instantiate (prefab, thePositionToMoveTo, Quaternion.identity);
}
}
Answer by rutter · Sep 04, 2014 at 10:40 PM
The third parameter to Instantiate is a Quaternion, which is a complicated data structure Unity uses to represent rotations.
You pass Quaternion.identity
, which is a rotation that's similar to Vector3.zero
(it applies no rotation).
Quaternions are difficult to manipulate directly, but you can construct them using Euler angles, which you're probably more used to:
//no rotation
Quaternion.identity
//rotate 20 degrees around y-axis
Quaternion.Euler(0, 20, 0);
For more information, you should probably look up a tutorial or check the scripting docs.
I'm hesitant to let this through the moderation queue because it's a frequently asked question. It's not a bad question, we're just trying to cut down on the number of duplicate threads in this area of the site. If you have anything else to add, feel free to comment here; if not, I'll drop the question from the queue in a little while.
If you can't find help, here, you can always try the forums or look up some tutorials online.
Your answer
Follow this Question
Related Questions
I need help with rotation 2 Answers
Instantiating objects at position 1 Answer
Get rotation from hit object 1 Answer