Start rotation Random Number

For the first day of using Unity it is a lot of fun. I am following some tutorials and reading through the docs, lots of doc’s. For a simple project I an dropping a cube on to another cube with collision detection and watching it bounce. The results every time I run it are identical which is expected. I decided to have a random number be generated to allow different results but it seems not to be working.

I assigned a script to the cube that drops. I can see that Debug.log is triggered and the result is random every time but the box seems to drop the same way every time? I am simply adjusting 1 axis.

#pragma strict
var startRotation:float = 45.0;

function Start () {
	startRotation = Random.Range(0.0, 359.0);
	 transform.rotation.x = startRotation;
	 Debug.Log("Fiered"+ startRotation);
         
}

function Update () {

}

Transform.rotation is a 4D quaternion…as the docs say, don’t try to modify a quaternion directly unless you understand them inside out. It can still be problematic to modify Transform.eulerAngles, so the best thing is to use Transform.Rotate.

–Eric

To set the eulerAngles, at least in C#, you can’t directly set them. You have to create a new Vector3 and assign that instead.

startRotation = Random.Range(0.0, 359.0);

Vector3 rotation = transform.eulerAngles;

rotation.x = startRotation;

transform.eulerAngles = rotation;

So if I understand it correctly, from the Doc’s, a Vector3 is a simple struct with x,y and z floats. So in my modified code, which works now. I am generating 3 random values for each axis. These are being assigned to the Vector3 struct which is assigned to the transform.eulerAngles.

function Start () {
	var startX:float = Random.Range(25.0, 300.0);
	var startY:float = Random.Range(25.0, 300.0);
	var startZ:float = Random.Range(25.0, 300.0);
	
	transform.eulerAngles = Vector3(startX,startY,startZ);
	 Debug.Log("Fiered"+ startX);
         
}