Unity Transform explained and how to use it in scripting

When you add a game object 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 scale of a game object in the 3D world.

Transform is the most basic, yet important component to know while learning Unity. You cannot define a game object in Unity without Transform. Even an empty game object has a 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 C# class like any other component in Unity. This class contains three important properties. These define position, rotation, and scale of an object.

Position defines the X, Y, and Z coordinates of the game object. Rotation is the degree of rotation of the game object in the X, Y, and Z axis. The scale is the size of the game object in these axes.

One important thing to take note here is, all these values are relative to the game object’s parent object. Say, the transform position of the game object is 0,0,5 and it’s a child of another object which is at the local 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 define the position of the object in the scene. We can access the values and store them in a Vector3 class as shown in the code below.

Vector3 pos=transform. Position;

You can use transform position in the following cases

  1. Find the location of any game object in the scene.
  2. To spawn a game object 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.

In Unity transform.position requires a Vector3 for a 3D object and a Vector2 for a 2D object.

The following code will demonstrate how to set a new position to an object using transform.

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

Above code moves the object from current position to 0,10,0 when the object/script is initialized.

Unity Transform Rotation

Rotation specifies the orientation of the game object 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 be afraid of the name Quaternion, it’s just a variable type to store the rotation data. If you want to understand quaternion and rotation in Unity, you can read this article on Unity Quaternion.

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.

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

The following code demonstrates transform.rotation.

quaternion rot;
float xrot;
float yrot;
float zrot;

void Start()
{
    rot=transform.rotation; //get rotation data and save in variable rot
    xrot=rot.x;
    yrot=rot.y;
    zrot=rot.z;
}

Void Update()
{
   xrot+=5; //increment x rotation by 5 degrees
   rot.x=xrot;
   transform.rotation=rot; //set rotation back to game object
}

This script takes the rotation of the object into a variable rot and then saves the individual axis rotation 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

Mostly the scale of the object 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 game objects.

So, unless you are very sure of the outcome of changing the scale, it’s better to set it beforehand. You can access the scale property using transform.scale.

Here is an example script on how to set the scale of an object

Vector3 newscale= new Vector 3(0,10,0);
Void Start()
{
transform. Scale= newscale;
}

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.position. 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. Here are the other properties of the Unity transform class.

Unity transform: other important properties

transform.Up

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

transform.Right

Returns the axis representing the right side of the game object. 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 game object 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 the game object to which the script is attached. You can find the game object’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 game object. Can be very useful to make your game object point towards the required direction.

transform.Rotate

Transform 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 an 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.

Now that you understand the basics of Unity editor and transform component, it’s time to venture into Unity Physics. Let’s start with Rigidbody.

4 thoughts on “Unity Transform explained and how to use it in scripting”

  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.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading