# Rotating A Cube Randomly

Hey there,

I am looking to make a minigame in which a cube rotates on any axis randomly. I would also like the rotation to speed up over time, just not toally sure how to do that in a script. Would something like a BodyVelocity be useful?

Let me know if you have ideas, thanks!

Use CFrame.Angles and math.random

``````local cube = path.to.cube
end
``````

What would be a way for me to adjust the speed? Also do you know of a video or a resource that I could use to better understand CFrame?

Just change the `math.random` with your speed

``````local cube = workspace.lolll
local speed = 0.5 -- Increase to make it fast
cube.CFrame *= CFrame.Angles(
)
end
``````

here are the few topics which will help you to learn CFrame
https://developer.roblox.com/en-us/api-reference/datatype/CFrame

You seem to know that rotation happens on an axis, very good.

To get such a random axis, you have to get a random direction.

A random direction is essentially a random point on a sphere.
This is significant because it’s easier to get good hits on Google if you ask it for this.
There is A LOT of garbage code online on this topic, some of them normalizing 3 random numbers into a unit vector, some of them doing some overly fancy Gaussian distribution stuff etc.

I also implemented pretty much the same stuff on that page sometime in 2017 or earlier according to this script I had laying around in my cavernous folders. I can’t read or paste the code because I don’t have Roblox on this computer.
module uniform 3d unit vector.rbxm (919 Bytes)

OK, so now you have a random axis to rotate around. What now?

If you’re OK with your big cube being unanchored, you can use a BodyAngularVelocity or its modern equivalent.
Set its value to (random direction) * (somewhat large number) for a few seconds. This will make it spin up, accelerating.
It might be wise to make it decelerate for the same amount of time so it doesn’t go too crazy, simply negate that value for the same amount of time, then stop the force.
For better results, have two or more such forces acting on the cube at once.
Also set the cube’s NetworkOwnership to the server so that an exploiter can’t make it go kaboom. (They can probably still fling it)
Also note that you might get two nearly collinear random directions, which might double up or cancel each other out, which will kill your players or bore them (to death).

To keep the cube anchored, you will have to keep in mind that if you just rotate the CFrame, you will also have to set the RotVelocity so that the cube does not simply slide out from under the players.

• Rotate the cube. `cube.CFrame *= CFrame.fromAxisAngle(axis, angle)`
• Match the RotVelocity. `cube.RotVelocity += axis * angle`