#9 Rotate objects in Unity. Rotate method of the Transform class.


In this arti­cle we're going to see how to rotate objects in Uni­ty. There are sev­er­al ways to achieve this, in this case we will use the Rotate method of the Trans­form class, this will direct­ly mod­i­fy the rota­tion para­me­ters that are observed at the top of the inspec­tor with the GameOb­ject selected.

Go to the project’s main page

Before we start I invite you to watch the video I made to sum­ma­rize this arti­cle. Eng­lish Sub­ti­tles available.




How are rotations expressed mathematically?

In order to describe math­e­mat­i­cal­ly we have to know direct­ly or indi­rect­ly the fol­low­ing parameters:

-Rota­tion axis

-Amount of angles to rotate

-Sense of rotation

Let's imag­ine that the object we want to rotate is a sphere. We take it in our hands and stick a rod through the exact cen­ter of the sphere. Then we take the rod and ori­ent it from north to south par­al­lel to the ground. Final­ly we make it turn counterclockwise.

Were you able to visualize it in your mind?

My descrip­tion of the rota­tion was accu­rate although per­haps it was not draft­ed in the best way to be under­stood. How­ev­er, let us assume that we could all have imag­ined exact­ly the same thing.

In the first place we can say that the axis of rota­tion is the rod. If we become strict the axis of rota­tion is an imag­i­nary line that pass­es through the two ends of the rod.

The num­ber of angles to rotate is half a turn, this math­e­mat­i­cal­ly is 180° or π radians.

The direc­tion of rota­tion is coun­ter­clock­wise or counterclockwise.

These three para­me­ters can be ful­ly described using a vec­tor, which is what we are going to do in this article.


We are going to use the rota­tion sta­tion which con­sists of three gears that are ori­ent­ed in the direc­tion of the world's three x,y,z axes.

exercise to rotate objects in unity 3d, box with golden gears to apply method rotate of class transform. The poster says rotate station
Fig. 1: Front view of the gear mech­a­nism to be used.

The three gears are posi­tioned per­pen­dic­u­lar to each other.

exercise to rotate objects in unity 3d, box with golden gears to apply method rotate of class transform. The poster says rotate station
Fig. 2: Side view of the gear mech­a­nism to be used.

In the hier­ar­chy we can find them inside the GameOb­ject "#9 Rota­teOb­jects". They have the names GearX, GearY and GearZ indi­cat­ing the axis of rota­tion of each gear.

Unity 3d hierarchy, three arrows point to the gears gameobjects
Fig. 3: In the hier­ar­chy we have a GameOb­ject for each gear.

The script to com­plete is called "Rota­teOb­jects" and is assigned to GameOb­ject with the same name in the hierarchy.

In fig­ure 4 we see that the three GameOb­jects of the hier­ar­chy are already assigned in the fields of the inspector.

component in inspector unity 3d. Script called rotate objects
Fig. 4: The gears are assigned in the fields of the Script Rotate Objects.

When open­ing the Script we find the FixedUp­date method defined and inside some com­ments that say that the gears must be rotat­ed there.

If it is not clear what a method is in my chan­nel there is a video in which I talk about meth­ods in pro­gram­ming (eng­lish sub­ti­tles avail­able) and there is also an arti­cle in the pro­gram­ming sec­tion of this page.

Why should the rotation code go in the FixedUpdate method?

In the Arti­cle 1 about read­ing key­board and mouse inputs we dis­cussed why read­ing instruc­tions were in the Update method.

The FixedUp­date method as its name indi­cates is an update of the object state but exe­cut­ed at a cer­tain fixed time. 

This means that no mat­ter how much the frame rate of the game changes, we will have the same num­ber of exe­cu­tions of this method per second.

Colo­care­mos en el méto­do FixedUp­date una instruc­ción que hará que los engrana­jes giren una deter­mi­na­da can­ti­dad de ángu­los en algún sen­ti­do respec­to de un eje. Esto sig­nifi­ca que en cada lla­ma­da de FixedUp­date se apli­cará la rotación, lo que gener­ará un movimien­to con­tin­uo de rotación.

variables and fields defined in c# script
Fig. 5: Fields defined in the Rota­teOb­jects Script.

c# script to rotate objects in unity 3d, fixedupdate method
Fig. 6: Script when first opened. The FixedUp­date method is defined.


The Rotate method of the Trans­form class has dif­fer­ent ver­sions, we are going to use one in which the first para­me­ter is the vec­tor that com­plete­ly express­es the rota­tion and in the sec­ond para­me­ter we indi­cate the type of coor­di­nates to use, glob­al coor­di­nates that are unique in the whole world of the game or local coor­di­nates that are spe­cif­ic to each par­tic­u­lar object, which would be as if each object had its own north.

script c# to rotate objects in unity 3d, fixedupdate method, object rotation
Fig. 7: Instruc­tions for rotat­ing each gear in rela­tion to its own axis.

By math­e­mat­i­cal prop­er­ties of the vec­tors I rewrote the vec­tor as the prod­uct of its mod­ule or norm mul­ti­plied by a unit vec­tor (norm 1) describ­ing the direction.

This allows us to sep­a­rate on the one hand the direc­tion of rota­tion and on the oth­er hand the num­ber of angles to rotate.

component in inspector unity 3d. Script called rotate objects to rotate objects in unity 3d
Fig. 8: Mod­i­fy­ing the vari­able in the inspec­tor we can change the speed of the set of gears.

Anoth­er advan­tage of express­ing the vec­tor that way is that we can change the direc­tion of rota­tion sim­ply by using neg­a­tive val­ues in the inspector.

component in inspector unity 3d. Script called rotate objects to rotate objects in unity 3d
Fig. 9: Using neg­a­tive val­ues the rota­tion of the gear set can be reversed.


You can sim­ply rotate objects in Uni­ty using the Rotate method of the Trans­form class.

If we don't need math­e­mat­i­cal pre­ci­sion, we can play with num­bers and get dif­fer­ent results.

With this type of rota­tion we direct­ly affect the rota­tion vec­tor of the Trans­form com­po­nent, this is fine if we seek to show only the ani­ma­tion rota­tion. If the rota­tion is need­ed to pro­duce phys­i­cal inter­ac­tions, Rigid­Body should be used.

Scroll to Top
Secured By miniOrange