askvity

How to Make a Cube Rotate in Unity?

Published in Unity Scripting 4 mins read

There are several ways to make a cube rotate in Unity, ranging from simple editor-based manipulation to scripting solutions for dynamic, game-controlled rotations. Here's a breakdown of common methods:

1. Rotating Manually in the Unity Editor

This is the simplest method, suitable for static scene setups or initial adjustments.

  • Select the Cube: Click on the cube object in the Scene view or Hierarchy window.
  • Use the Rotation Gizmos: Once selected, rotation gizmos (colored arcs) will appear around the cube. Each arc corresponds to a different axis of rotation (X, Y, and Z).
  • Drag the Gizmos: Click and drag one of the gizmo arcs to rotate the cube around that axis.
  • World vs. Local Space: By default, the gizmos align with the World axes. You can switch to Local axes (relative to the cube's current orientation) in the toolbar above the Scene view (look for the "Global" or "Local" toggle next to the coordinate handle selection).

2. Rotating with a Simple Script (C#)

This method uses a C# script attached to the cube to rotate it continuously. This is the most common and versatile approach for game development.

Creating the Script

  1. Create a New C# Script: In your Unity project, create a new C# script (e.g., "RotateCube").
  2. Write the Rotation Code: Open the script in a code editor (like Visual Studio) and add the following code:
using UnityEngine;

public class RotateCube : MonoBehaviour
{
    public Vector3 rotationSpeed = new Vector3(15, 30, 45); // Degrees per second

    void Update()
    {
        transform.Rotate(rotationSpeed * Time.deltaTime);
    }
}

Explanation of the Code:

  • using UnityEngine;: Imports the necessary Unity engine namespace.
  • public class RotateCube : MonoBehaviour: Defines a class named RotateCube that inherits from MonoBehaviour. This makes it a script that can be attached to a GameObject in Unity.
  • public Vector3 rotationSpeed = new Vector3(15, 30, 45);: Declares a public Vector3 variable named rotationSpeed. This variable determines the speed of rotation around each axis (X, Y, Z) in degrees per second. The values (15, 30, 45) are just an example; you can adjust them in the Unity Inspector.
  • void Update(): The Update() function is called once per frame.
  • transform.Rotate(rotationSpeed * Time.deltaTime);: This is the core of the rotation logic.
    • transform refers to the Transform component of the GameObject this script is attached to (in this case, the cube).
    • Rotate() is a function of the Transform component that rotates the object.
    • rotationSpeed * Time.deltaTime calculates the amount to rotate in the current frame. Time.deltaTime provides the time elapsed since the last frame, ensuring consistent rotation speed regardless of frame rate.

Applying the Script to the Cube

  1. Attach the Script: Drag the "RotateCube" script from your Project window onto the cube object in the Hierarchy window.
  2. Adjust Rotation Speed (Optional): In the Unity Inspector (with the cube selected), you'll see the "Rotate Cube (Script)" component. You can modify the rotationSpeed values in the Inspector to change the rotation speed and direction around each axis.

3. Rotating Using RotateAround

The RotateAround function allows you to rotate the cube around a specific point in world space.

using UnityEngine;

public class RotateAroundPoint : MonoBehaviour
{
    public Transform pointToRotateAround;
    public float rotationSpeed = 20f;

    void Update()
    {
        if (pointToRotateAround != null)
        {
            transform.RotateAround(pointToRotateAround.position, Vector3.up, rotationSpeed * Time.deltaTime);
        }
    }
}

Explanation

  • pointToRotateAround: This public variable allows you to assign another GameObject in the scene, and the cube will rotate around its position.
  • Vector3.up: Specifies the axis of rotation (in this case, the Y-axis). You can change this to Vector3.right (X-axis), Vector3.forward (Z-axis), or any other Vector3 to customize the rotation axis.

To use this script:

  1. Create an empty GameObject in your scene to serve as the rotation point.
  2. Attach the RotateAroundPoint script to your cube.
  3. Drag the empty GameObject from the Hierarchy window to the Point To Rotate Around field in the Inspector for the RotateAroundPoint script attached to the cube.
  4. Adjust rotationSpeed in the Inspector as needed.

4. Rotating with Quaternion.Euler and AddTorque

These are more advanced techniques. Quaternion.Euler is useful for setting specific rotations programmatically, while AddTorque applies a force to the cube, causing it to rotate based on its physics properties (if a Rigidbody component is attached).

Related Articles