- Home /
Random rotation of instantiated prefab along one axis
http://answers.unity3d.com/questions/20706/random-rotation I know this question has been raised once before although no clear answer was made
I'm trying to rotate a instantiated prefab along just the x axis at random, i've tried the Random.rotate but of course it rotates along all on the axis. I understand that i have to use Quaternion.eulerAngles but I'm still confused.
var grass : GameObject;
function Start() { var grassNum = Random.Range(200, 300); for (var i = 0; i < grassNum; i++){ var position = Vector3(Random.Range(2, 6), 1.5, Random.Range(2, 6)); Instantiate(grass, position, Quaternion.identity); } }
Thanks - C
Answer by kkaploon · Feb 20, 2011 at 08:48 PM
How about something like that:
var grass : GameObject;
function Start() { var grassNum = Random.Range(200, 300); for (var i = 0; i < grassNum; i++){ var position = Vector3(Random.Range(2, 6), 1.5, Random.Range(2, 6)); var tempObj = Instantiate(grass, position, Quaternion.identity); tempObj.transform.eulerAngles.x = Random.Range(0, 360); } }
Answer by Peter G · Feb 20, 2011 at 09:47 PM
If you don't need an object reference, then you can instantiate the object after you create a rotation.:
var grass : GameObject;
function Start() { var grassNum = Random.Range(200, 300); for (var i = 0; i < grassNum; i++){
var randomPosition = Vector3(Random.Range(2, 6), 1.5, Random.Range(2, 6));
var randomRotation = Quaternion.Euler( Random.Range(0, 360) , 0 , 0);
Instantiate(grass, randomPosition, randomRotation);
}
}
And are you sure that you want to rotate around the x-axis? I would guess you want the y-axis which passes vertically through the object so you could spin it like a top and the bottom would stay flat.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105403im_/https://answers.unity.com/themes/thub/images/avi.jpg)