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 is 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.

What is Transform in Unity?

Unity Transform is a class like any other component in Unity. This class contains three important properties. 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 Ven diagram

Unity Transform major properties

Unity Transform position

Position as the name suggests 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.

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

in Unity 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 examples are below

  1. Rotate the camera in the 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 needed 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 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’s 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 Unity Transform but this summarizes the basics you need to understand to make simple games. If you have any queries leave them in the comment below.

Unity transform other important properties

Transform.Up

Returns the axis representing the top of the gameobject. In other words, the red axis in the gizmo.

Transform.Right

Returns the axis representing the right side of the gameobject. Unlike Vector3 in Unity, transform considers the rotation of the game object.

Transform.forward

Returns the direction in which the game object is facing. You can use this to move the object in the direction it’s facing.

Transform.parent

Returns the parent of the game object.

Transform.eulerAngles

Returns the rotation of the game object in Euler angle degrees.

Transform.childCount

Returns the number of children the gameobject has in the hierarchy.

Important public methods of Unity transform

Transform.Find

This method is very similar to Gameobject.find but can only be used to find the child of this gameobject. You can find the gameobject’s child using its name in this method. You can also find the child using Transform.GetChild but it takes the index as input.

Transform.Lookat

This is used to set the forward direction of the gameobejct. Can be very useful to make your gameobject point towards the required direction.

Transform.Rotate

Tranform rotation which we came across in the beginning takes a quaternion as input. In order to set the rotation by a few Euler angles you can use Transform.Rotate. You can also use Transform.RotateAround to rotate an object around a axis.

Transform.Translate

It’s used to move an object in Unity in the direction of a vector at a speed equal to the magnitude of the vector.

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.