How to rotate an object around a point in Unity

You can rotate an object on its own axis using transform rotate but if you want to rotate an object around a different point and along a custom axis then this tutorial is for you.

I am using Unity 2021.1.13 for this tutorial.

We have two objects, a cube and a sphere, in our scene. Let’s rotate the cube around the sphere for the purpose of this tutorial.

We will be using “transform.rotate” around for this purpose. It takes in 3 arguments.

The first one is a vector3 point in space around which you want the object to rotate. The second argument is the axis and the third one is the angular speed.

Syntax:

ransform.tRotateAround(Vector3 point, Vector3 axis, float angular_speed);

Let’s add the script below to our cube object. Make sure the sphere and the cube are not in the same position.

Select the cube and go to the Inspector window.

Click on add component and select new script.

Name the script as move_around

Copy and paste the code below into the script.

using UnityEngine;

public class move_around : MonoBehaviour
{
    public GameObject sphere;
    float angular_speed=20f;

    // Update is called once per frame
    void Update()
    {
        transform.RotateAround(sphere.transform.position,Vector3.up,angular_speed*Time.deltaTime);

        
    }
}

Here is the output

If you change the axis from Vector3.up to Vector3.forward

The cube starts rotating around a different axis

You can also give in a custom axis based on your requirement.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.