- Home /
Instantiate an object with same rotation
Hello! I need to instantiate a cylinder but it has a rotation of 90 degrees on the X-axis, so it looks like a log. When it spawns, it start to roll. I can't figure out how to instantiate it and then have it have its rotation. Also, I saw a trick where if you put the object in an empty game object and instantiate the empty one, it will keep its rotation, however, it gets really confusing and I'd rather not do that. Can anyone get me a solution? Thanks!
Answer by Exonto · Jan 02, 2018 at 05:31 PM
Inside the instantiate method, you can specify both the position and rotation of the object you are instantiating.
Instantiate(theGameObject, thePositiion, theRotation);
The rotation will be specified with a Quaternion.
Answer by Carson365 · Jan 02, 2018 at 07:09 PM
How would I write it? I am new to coding so I am not sure how to do it. I need the rotation to be 90 on the X, and 0 on the z and y.
In your case you should probably use "euler angles". This is a fancy way of saying x, y, and z angles (as opposed to a Quaternion which takes in four values rather than 3). So basically your goal is to create a Quaternion by providing the x, y, and z angle since that is what Instantiate requires. Here's how you can do that:
var rotation = new Quaternion();
rotation.eulerAngles = new Vector3(x, y, z);
Instantiate(theGameObject, thePosition, rotation);
I'm not able to test it at the moment but this should work just fine. Hope this helped.
Your answer
Follow this Question
Related Questions
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Store Game Object Into List For Later Reinstantiation? 0 Answers
Destroying childs and Instantiate [C#] 0 Answers
How to randomly spawn three non repeating gameobjects from an array? 2 Answers
How can I spawn gameObjects within a circle radius? 1 Answer