Before updating to Windows 10 you may need to return your Pro license. More information here

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

avatar 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

avatar image

565 5 10 25

But how would you get this single script to work on multiple game objects?

5 days ago babelbrian
(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: 5685 times

Last Updated: 5 days ago