Rotating an Object around itself

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 start method.

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

Inside the update we add a call to the Rotate function 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.