Random Rotation of an Instantiated Game Object

Hi,

Problaby a simple question, but I’m a bit stumped for the right method.

I’m making a simple top-down / verticle space game.

I have randomly instantiated rigidbody asteroids that appears at the top of the screen, which then travels down, when they are out of bounds they are destroyed.

Could somone help me script a random roation (x, y, z) of the asteroid when they are instantiated - thus allowing them to then have a constant roation then set as they travel down the screen?

I’ve looked at Random.Range, but I don’t know how to implement it in this case.

I assume that on the Start method (or maybe Awake) is where I create the random axis to spin. Then in the Update (possible Fixed Update) is where I keep applying the rotation from the randomly generated axis.

Maybe I’m not on the right track?

Thanks,

Jay.

If you’re blasting asteroids in space, you’re ALWAYS on the right track.

You can probably just set the angular velocity of the Rigidbody to something random when you spawn them and let the physics take care of it.

myAsteroidRigidbody.angularVelocity = Random.onUnitSphere *
    Random.Range( 10.0f, 100.0f);  // min / max random speed

EDIT: set the angularDrag to zero otherwise they’ll slow to a stop:

myAsteroidRigidbody.angularDrag = 0;
2 Likes

Thanks, tested it out today, works like a charm!

I placed it the Start method and I also used Random.range to vary the size of said asteroids.

Cheers,

Jay.

1 Like