Unity Transform explained in simple terms

When you add a Gameobject in the Unity scene the first thing that you notice in the inspector window is the transform component. Unity transform defines the position, rotation, and size of a Gameobject in the game world. transform might be the most basic component in Unity but it’s also the most important. You cannot define a Gameobject in Unity without transform. Even an empty Gameobject has the transform component. In this post, we will see what the Unity transform component does and how you can use it in your game.

Unity Transform is a class like any other component in Unity. This class contains three important properties or functions. These are position, rotation, and scale. Position defines the X, Y, and Z coordinates of the Gameobject. Rotation is the degree of rotation of the Gameobject in the X, Y, and Z-axis. The scale is the size of the Gameobject in these coordinates. One important thing to take note of here is all these values are relative to the Gameobject’s parent object. Say, the transform position of the Gameobject is 0,0,5 and it’s a child of another object which is at the position 0,0,5 then the overall position of the child is 0,0,10 from the origin.

Unity Transform

Unity Transform Uses

Unity transform position

Position as the name suggest is used to change the position of the object in the scene. But it cannot be used to move players with physics as when you change the transform position the object is teleported from the initial position to the target position. So where will transform position be useful? You can use it in the following cases

  1. Find the location of any Gameobject in the scene.
  2. To spawn a Gameobject to a particular location.
  3. Move an object without satisfying physics laws.
  4. To make the camera follow the player.
  5. Define target location of moving objects.

Example script

Transform position requires a vector3 for a 3D object and a vector2 for a 2D object.

vector3 newpos= new vector3(0,10,0);
Void Start()
{
transform.position= newpos;
}

Above code moves the object to 0,10,0 when the object is initialized.

Unity Transform Rotation

Rotation specifies the orientation of the Gameobject in the scene. Depending upon the axis of rotation the value of the X, Y, and Z coordinates will vary. Rotation is described in Euler angles in Unity and quaternion is used to store the data. Don’t get afraid of the name Quaternion it’s just a variable type to store the rotation data. If you want to get into the mathematics part of quaternion you can read this article from mathworld.

Rotation can be used for many things in a game. Some come examples are below

  1. Rotate the camera in game to change the view.
  2. You can spin an object continuously using rotation.
  3. Rotate light to get day and night cycle.
  4. Get the object orientation.
  5. Tilt an object.

Sample Rotation script

Even though a quaternion is need to save the rotation data you can save individual axis data in float variable.

quaternion rot;
float xrot;
float yrot;
float zrot;
void Start()
{
rot=transform.rotation;
xrot=rot.x;
yrot=rot.y;
zrot=rot.z;
}
Void Update()
{
xrot+=5;
rot.x=xrot;
transform.rotation=rot;
}

This script takes the rotation of the object into a variable rot and then saves the individual axis data in xrot, yrot and zrot when the script is initialized. It then rotates the object by 5 degrees every frame along the x axis.

Unity Transform Scale

Transform scale is the least used transform. Mostly the object transform is set at the beginning in the inspector window and then left untouched. The reason why the scale is not disturbed during gameplay is it can make the object look absurd and might interfere with other gameobjects. So, Unless you are very sure of the outcome of changing the scale it’s better to set it beforehand.

Understanding Local Transform

In Unity or 3D space in general, there are two spaces used for reference. One is global space and the other is local space. Let’s see an example to understand it better. Say there is a ball in the coordinate 10,0,0 and you want to move it to 0,0,0 in the world space. But the ball has a parent to it which is in the coordinate -20,0,0. Now to move the ball to 0,0,0 you must know the object location with respect to world space.

The coordinates of the child are always with respect to the parent. So the actual position of the child ball object in world space is -10,0,0 but in local space, it is 10,0,0. So to move the object to 0,0,0 in the world space you need to add 10,0,0 to the position of the child.

In Unity, this difference is obtained using the local keyword. If you want to move the object relative to the parent then you should use transform.localPosition rather than transform.postion. Local space can be confusing in the beginning but you will understand it as you apply it in your game.

There are many other features of the Unity Transform but this summarized the basics you need to understand to make simple games. If you have any queries leave them in the comment below.

4 thoughts on “Unity Transform explained in simple terms”

  1. Who can explain Transform better than you..!
    What a way to deliver a systematic concept.Really loved the way of explaining in well arranged and planned manner.

    Reply
  2. Thank you Sir.
    You cleared all the doubts which were the hurdles for me everytime I tried to do my work in gaming sector.This information was really helpful for me and please keep updating and posting these type of stuffs on your website.These are really good source to learn new things.

    Reply

Leave a Reply

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