How to Make an Object Orbit Another in Unity
Would you like to make one object orbit another object in Unity like the earth revolves around the sun?
To make an object orbit, or revolve around, an object, use the RotateAround() function on the gameobject that is revolving around another object, or point. Specify parameters for the target the gameobject will revolve around, which direction it will revolve, and how fast it will move.
The object revolving around, or rotating around, the other object will have a script attached to it. You will then reference the stationery object in the moving object’s script. Here is an example of this.
How Do I Use RotateAround() in Unity?
Here is the the syntax for the RotateAround() function in Unity:
public void RotateAround(Vector3 point, Vector3 axis, float angle);
public void – Declaring something as public allows other classes that do not include this function to be able to access it an use the function. Void means that this function will not return any value.
RotateAround(Vector3 point, Vector3 axis, float angle); – The RotateAround() function takes three parameters used when the function is called. The first is a Vector3 position. This position specifies which location in the scene the object will orbit, or revolve around. The next parameter is another Vector3. This parameter specifies around which axis your object will orbit. You can use the x, y, or z axes. (Can use 2 axes at once?)
Finally, the third parameter is for how many degrees around a circle, your object will pass. This can be multiplied by Time.delta time allowing you to move your object at different speeds.
Rotate One Object Around Another Object
This is an example of an object revolving around another object using RotateAround(). It includes two spheres with one revolving around the other. The sphere that is moving will have a script attached to it. The other sphere does not need a script.
The blue sphere will have a script file attached to it to make it move around the red sphere in a circular motion. Here is the code below. We will explain the parts specific to this example.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateAround : MonoBehaviour
{
[SerializeField] private GameObject target;
[SerializeField] private float degreesPerSecond = 45;
// Update is called once per frame
void Update()
{
transform.RotateAround(target.transform.position, Vector3.forward, degreesPerSecond * Time.deltaTime);
}
}
[SerializeField] private GameObject target; – This line starts with [SerializeField], which is a class that allows a private field to be shown in a the editor. ‘Private’ disallows outside classes from accessing this field. ‘GameObject’ is the type of data structure that will be held in the field. Finally, ‘target’ is the name of the field.
‘target’ will hold the sphere gameobject that will be stationary. We will use this reference in our Inspector window so that our script knows which object to rotate around.
[SerializeField] private float degreesPerSecond = 45; – This line declares a private float variable named degreesPerSecond and initializes the value to 45.
transform.RotateAround(target.transform.position, Vector3.forward, degreesPerSecond * Time.deltaTime); – The Update() function is where we make the object move. We access our spheres position by going into its transform, the running the RotateAround() function. You can see the three parameters that this function takes in.
target.transform.position – This parameter gets the Vector3 position of the object to which this script is attached. So it will have an X, Y, and Z position.
Vector3.forward – This parameter takes in the the axis to rotate around. In this case, it uses ‘.forward’ which means the Z axis. Moving along the positive Z axis is considered moving forward. This could have been written as ‘new Vector3(0,0,1)’, and it would have done the same thing.
degreesPerSecond * Time.deltaTime – This last parameter specifies the speed in which the gameovject, using RotateAround(), will revolve around another object. In this example we use 45 as how many degrees we want the gameobject to revolve in one second. We do this by multiplying 45 by Time.deltaTime.
When you multiply a number by Time.deltaTime, you can assign different speeds to a gameobject. In this case, since we multiply 45 by Time.deltaTime, our sphere will revolve around the other sphere at a speed of 45 degrees per second. Circles are divided into 360 degrees, so if we take 360 and divide it by 45, we get eight. Eight is the number of seconds it will take our moving sphere to revolve around the other sphere once.
Make sure the script is attached to the sphere that will move, and that is all you need to make it rotate around the other object. You can change the direction your moving object follows by changing the second parameter in the RotateAround() function. If you want it to revolve around the Y axis, use Vector3.up, and if you want it to rotate around the X axis, use Vector3.right.
Note that you can change the direction of the rotation. In this example, the easiest way to do it is to use the minus (-) sign like it is used below:
transform.RotateAround(target.transform.position, -Vector3.forward, degreesPerSecond * Time.deltaTime);
As you can see, putting the minus sign in front of the direction parameter is all you need: ‘-Vector3.forward’. Note that this is the same as having the parameter be the following code:
transform.RotateAround(target.transform.position, new Vector3(0,0,-1), degreesPerSecond * Time.deltaTime);
There is a minus sign in front of the Z parameter of the Vector3. This means that it will revolve around the Z axis going the opposite direction.
Also, if you do not use Vector3.forward and instead use Vector3(0,0,1) in this example, you can change the values for the X and Y axes to 0 or 1 and you can also change the direction of each individual axis. This will allow your object to orbit around something using multiple axes.
How Do You Rotate a Camera Around an Object?
As another example, if you would like to rotate a camera around an object while looking at it, you can do that as well.
To rotate a camera around and object while looking at it, use the RotateAround() function. Using just the RotateAround() function, you need to make sure the camera is in the correct location, looking at the object you want it to rotate around. Here’s the example script.
public class CameraRotate : MonoBehaviour
{
[SerializeField] private GameObject target;
[SerializeField] private float anglesPerSecond = 45;
// Update is called once per frame
void Update()
{
transform.RotateAround(target.transform.position, Vector3.up, anglesPerSecond * Time.deltaTime);
}
As you can see, this is the same code we used in the previous example. It is attached to the scene’s camera. You create a reference for a gameobject, which will be the object the camera is looking at. You then declare and initialize the variable for how many degrees per second the camera will rotate around the object.
Then you use the RotateAround() function, with the first parameter being the object the camera is rotating around, the next parameter is which axis the camera is revolving around, and the third is how many degrees the camera is moving per second, which we get by multiplying our variable by Time.deltaTime.
There you have it. RotateAround() is just one of many very useful functions in Unity. Another very useful function is the LookAt() function, and it can be used in conjunction with the RotateAround() function. For some information on the LookAt() function, check here.