0 of 16 lessons complete (0%)
Essential Activities

Rotating an Object around itself

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 20 minutes

Here we will write a script to rotate an object around itself.

Create a scene with an object that you would like to rotate on it.

In this case[2] we will be rotating the Cube, and the sphere that is nested inside it be association.

Create a new script RotateObject.

Delete the start method.

Create a public float[3] for the speed of rotation to use. This is shown on line 7.

Inside the update we add a call to the Rotate function[1] of the transform for the object.

We are modifying the rotation around the x, y and z axis. We can access this through Vector3.right (x), Vector3.up (y), and Vector3.forward (z). We multiply this by the RotationSpeed to set the speed of rotation and Time.deltaTime to make the animation smooth in time with the framerate.

The final argument is Space.Self which ensures that we rotate the object around itself rather than the World Space.

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

public class RotateObject : MonoBehaviour
{
    public float RotationSpeed = 20.0f;

    // Update is called once per frame
    void Update()
    {
         transform.Rotate (Vector3.right * RotationSpeed * Time.deltaTime +
                        Vector3.up * RotationSpeed * Time.deltaTime +
                        Vector3.forward * RotationSpeed * Time.deltaTime, Space.Self);
         
    }
}

Once you have this script written attach it to the object you want to rotate.

Run your program to check that it works.

You can also attach the script to other object to rotate them as well.

Terms definitions
2. case. A control[6] mechanism in which the value of a variable[4] or expression changes the execution of a program and which usually involves multiple branches.
3. float. A data type[5] used to store a floating point decimal number.
Terms definitions
1. function. Length: 20 minutes Here we will write a script to rotate an object around itself. Create a scene with an object that you would like to rotate on it. In this case we will be rotating the Cube, and the sphere that is nested inside it be association. Create a new script RotateObject. Delete the […]
2. case. Length: 20 minutes Here we will write a script to rotate an object around itself. Create a scene with an object that you would like to rotate on it. In this case we will be rotating the Cube, and the sphere that is nested inside it be association. Create a new script RotateObject. Delete the […]
3. float. Length: 20 minutes Here we will write a script to rotate an object around itself. Create a scene with an object that you would like to rotate on it. In this case we will be rotating the Cube, and the sphere that is nested inside it be association. Create a new script RotateObject. Delete the […]
4. variable. Length: 20 minutes Here we will write a script to rotate an object around itself. Create a scene with an object that you would like to rotate on it. In this case we will be rotating the Cube, and the sphere that is nested inside it be association. Create a new script RotateObject. Delete the […]
5. data type. Length: 20 minutes Here we will write a script to rotate an object around itself. Create a scene with an object that you would like to rotate on it. In this case we will be rotating the Cube, and the sphere that is nested inside it be association. Create a new script RotateObject. Delete the […]