[Unity] Script to Roll A Cube

[Unity] Script to Roll A Cube

This is the story to make “Colorful Dice”. It is not so easy to roll a cube.

First of all, I thought it was easily resolved by physics engine of Unity. But it was not easy.



Make a plane and a cube as above, and put the following script and Rigidbody property to the cube.

It can roll the cube as shown in the following movie.


However, the values after moving were not good. They were little bit different from round number.


In this situation, The prospect values were

Position: (0, 0.5, 1)

Rotation: (90, 0, 0)

I guess the differences were due to the friction between the cube and the plane, bounding when the cube touch the plane. Moreover, to use angularVelocity is not recommended in the script reference of Unity because it can result in unrealistic behavior. Actually, it is impossible that a stopped car suddenly has a velocity of 100 km/h. There is a function of “addTorque”, but I guess the result would be the same. The difference is small but it get large after repeating. The higher the rolling velocity is, the larger the difference is. When the speed is too high, the cube jumps (It’s funny as shown in the following movie).


So, I don’t realize the rotation of cube using the physics engine. I wrote a script to control the position and rotation. It is easy mathematics.


I wrote the following script according to the upper equations.

Check “IsTrigger” of the cube’s “BoxCollider” and uncheck “UseGravity” of “Rigidbody”. You can see the result.


I could not imagine to roll a cube take such the work.


My app to roll a dice, “Colorful Dice” is available from the following.

【Android】Colorful Dice