Tag

Transform Archives - Selfloop

Understanding : Transform, position, scale, rotate (Unity)

By | Unity3D | No Comments

Definition: Almost all the “GameObject” has a “Transform” component as its responsible to denote the position, rotation and scaling in the game world, where these values can be changed in 3 different axis (x, y, z).


Let’s break down each part for our own better understanding and see the usage of the “variables” & “function” that are mostly used from the “Transform” component.

  • transform: when we write “transform” in our script, it takes the reference of the “Transform” component that is attached with the “GameObject”. Let’s see the picture,

1

Here, the “Stick Man” game object has transform component which we can see in the “Inspector” view. When we write “transform” in our script, it asks the game object to pass him the reference of its own “Transform” component. So any change that you make with the “transform” in your script, it will make the change of “Transform” component of the “GameObject” (Stick Man) in the game world.


  • position: It is used to get/set the position of the “GameObject”. The “transform.position” takes “Vector3” as the argument. So now the question might be come, what is “Vector3”? Well, “Vector3” is the set of 3 variables, who represent the 3D vectors & points in the game world. Let’s see the picture below,

2

Here we can see the “Vector3” representation of the “Stick Man” “GameObject in the game world. So if we write “Debug.Log(transform.position);” in our script, we could see “(2,2,0)” as our output in the Unity Log.

 

But what if we want to change the position through the script, isn’t there anyway to do that? Yes, there are. If we would like to change the position through our script, we can use any of the following 2 ways to change the position.

position = new Vector3(5,4,3);

Vector3 ourNewPosition = new Vector3(5,4,3);

transform.position = ourNewPosition;


localPosition: It is used to get/set localPosition of the “GameObject” respect to its parent. Let’s see the following figure.

3

As we can see, the “transform.position” is showing the position of the “Hat” respect to the axis of the game world, where the “transform.localPosition” is showing the position of the “Hat” respect to “Stick Man” position as it’s the child of the “Stick Man” game object.


  • eulerAngles: It represent the rotation of the “GameObject” with respect to X/Y/Z axis in degrees. The “eulerAngles” take the argument as Vector3, where the X-represent the degrees around the X-Axis, Y-represent the degree around the Y-Axis and Z-represent the degree around the Z-Axis. I usually use “eulerAngle” when I want to read the rotation in degree around the Axis in world space. To simply read the rotation type,

 

Debug.Log(transform.eulerAngles);

 

Which will show rotation in degrees of the gameObject where the script has been attached.


  • Rotate: This function is responsible for rotating the gameObject over the x, y & z axis. “transform.Rotate” takes “Vector3” as the argument. So if someone write down “transform.Rotate(0,0,1)”, it means there is “1” degree of rotation over the Z-axis. One of the best practice I use to declare a “rotationSpeed” from the inspector, then by using
  • Vector2.left: (-1,0,0),
  • Vector2.right: (1,0,0),
  • Vector2.up: (0,1,0),
  • Vector2.down: (0,-1,0),
  • Vector2.forward: (0,0,1),
  • Vector2.back: (0,0,-1),

I decide which direction the gameObject will be rotate. So by simple typing down these to my script,

public float rotationSpeed;

                void Update(){ 

                transform.Rotate(Vector3.forward * rotationSpeed);

}

It will simply rotate the gameObject over the positive Z-axis with the declared rotation speed that has been assign in inspector.

Note: Like the “transform.localPosition” , we also have the “transform.localEulerAngle” for get/set the rotation respect to its parent. Let’s see some of the figure about the rotation in the given below.

Rotation X-AxisRotation Y-AxisRotation Z-Axis


  • transform.scale: It represent the scaling of the gameObject over the X,Y & Z axis. “transform.scale” also takes argument of “Vector3” where the “Vector3.x” represent scaling over the X-Axis, “Vector3.y” represent scaling over the Y-Axis & “Vector3.z” represent scaling over the Z-Axis.

So for example, if we want to scale a game object 50% over X-axis, then all we have to do is to write (we are considering the current scale as (1,1,1). )

transform.scale = new Vector(1.5,1,1);

  • localScale: It is used to get/set localScale of the “GameObject” respect to its parent. Let’s see the following figure.

Scaling X-AxisScaling Y-AxisScaling Z-Axis


# The reference of the documents are taken from the “Unity – Documentation”.