- Home /
Question by
Ilez12 · Mar 04, 2020 at 05:50 PM ·
rotationscripting problemfloat
How to make an object rotate on randon axes when spawned?
Hello! So i have this very simple "transform.Rotate(new Vector 3 (0, Rotation,0));" code inside Update that makes the object rotate. I want to add few more lines like this but with different values of Vector 3 and make Unity randomly select one of them if object is spawned . I guess it supposed to be pretty easy but it is not for me... Can anyone help?
Comment
Best Answer
Answer by pete94za · Mar 04, 2020 at 07:13 PM
Use a switch statement along with Random.Range like so:
int randomXYZ = Random.Range(0, 3); // int random.range excludes the last number so it will go from 0 to 2
switch (randomXYZ)
{
case 0:
transform.Rotate(new Vector3(Rotation, 0, 0));
break;
case 1:
transform.Rotate(new Vector3(0, Rotation, 0));
break;
case 2:
transform.Rotate(new Vector3(0, 0, Rotation));
break;
}
Answer by austephner · Mar 04, 2020 at 06:57 PM
You can use Random.Range(...)
to help generate random numbers for rotating. Range takes two parameters, a min and max. Generate three random values in this manner to populate a whole Vector3.