Creating A Rotating Cube With Unity3D

In This Tutorial, We’re going to create a rotating cube. I’m using Unity3D 5.5.0f3 for this tutorial and you can download the latest version from Unity3D official website.

Open Unity3D and click on “New”

In the next window, set your project’s name as “RotatingCube” and set your project location where you want as well. Then leave everything unchanged. Finally Click on “Create Project”

Well, now we just created our project and we’re ready to go. In this step, we have no scene in the project and we need to have one.

Click on File => Save Scenes, Set your file name (scene name) as “Main” then click on save button.

Now we have our scene saved on the Asset folder in Project tab. Our scene is ready now we have to create our cube, For this purpose click on GameObject => 3D Object => Cube

As you can see, there is a cube in the scene tab and a cube game object in the hierarchy tab

Select the cube game object in the hierarchy tab, then you see properties of this game object in the inspector tab. while selecting the cube, You see multiple components for the cube like Transform, Cube Mesh, Box Collider and Mesh Renderer in the inspector. Notice the little gear icon in the top-right corner of each component. Click on the little gear of Transform component and choose Reset. This will reset the Position and Rotation properties to (0, 0, 0) and Scale property to (1, 1, 1). Now we’re in the center of our world.

To have a better view in Game tab, Set your Main Camera’s transform properties to Position (4.5, 3, 5) and Rotation (24, -136, 0).

It’s time to rotate the cube. For rotating our game object we need to write our script and attach it to the cube.

In the Project tab, Right click on the Asset folder then choose Create => C# Script, then rename the script to CubeRotator.

Select the cube game object then drag the script into the inspector tab to attach the script to the cube game object. Double click on the script to open it.

 

Consider the default code in the CubeRotator script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeRotator : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

Replace this code with the code below

using UnityEngine;

public class CubeRotator : MonoBehaviour
{
    public float speed = 100;

    void Update()
    {
        transform.Rotate(new Vector3(0, speed * Time.deltaTime, 0));
    }
}

We just deleted the Start method because we don’t need it in this script. We have defined a speed variable to control the speed of rotation. In the Update method we have added this line of code

transform.Rotate(new Vector3(0, speed * Time.deltaTime, 0));

The Rotate method of the transform property applies a rotation around the x, y or z axis. If we give the method a Vector3 like the above code, Rotation will happen around the y axis. You ask, Why we multiply speed variable by Time.deltaTime? Actually we’re doing this to make our game frame rate independent. It means if my computer runs this example 60 fps and your computer runs it 30 fps, The cube rotation speed does not change!

Now play your game and watch the rotating cube

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s