One Script / Random Rotation / Multiple Objects

[newbie - 1 day of experience]

I have 9 cubes on screen (grid 3x3).

I want to keep rotating them in different directions at different speeds.

I made a single script and assigned it to all 9 cubes.

void Update ()
{ transform.Rotate(Random.value / 4.0f, Random.value / 4.0f, Random.value / 4.0f); }

But all the cubes spin in the same direction? My guess is the problem might be single evaluation of the script??

Any thoughts/solutions?

more ▼

asked Mar 03, 2010 at 10:25 AM

JanV gravatar image

17 6 6 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You are on the right track but forgot a couple of things. Your algorithm will actually vary the rotation as each cube will show discrepancy in rotation over time. The problem is that they will always move in the positive direction along the x,y,z axes because Random.value is a value generated between 0.0 - 1.0

This is how you get both positive and negative rotations, as well as greater randomness in axes:

private var x = Random.value - 0.5; private var y = Random.value - 0.5; private var z = Random.value - 0.5;

function Update() { transform.Rotate(x, y, z); }

Simply set the x,y,z in the Update() loop if you want a jittery effect instead.


You can also simply do this:

transform.rotation = Random.rotation;

But I doubt you'll want to change that every frame as it'll be hyper. It is good to use in the Start() function to set an initial random object rotation.


more ▼

answered Mar 03, 2010 at 12:16 PM

Motionreactor gravatar image

545 5 8 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 03, 2010 at 10:25 AM

Seen: 5208 times

Last Updated: Mar 03, 2010 at 10:25 AM