The 'Transform' class in Unity is the invisible puppet master that dictates where and how GameObjects exist within the game's spatial dimension. It's a control hub for manipulating the position, rotation, and scale of GameObjects, and where they fit into the game scene's hierarchy.
Think of GameObjects as characters on a theatre stage - While components like Mesh and Material determine what the characters look like, 'Transform' is the stage director, instructing them where to be, which way to face, and how large their presence is. The position of a GameObject sets its location in 3D space, the rotation controls the direction the object is facing, and the scale dictates the size of the GameObject. Including local and global space settings, 'Transform' caters for both simple and complex movements, rotations, and scaling.
Not just limited to single GameObjects, 'Transform' also enables hierarchy settings for GameObject relationships, like setting one GameObject as the child of another. These relationships form a tree-like hierarchy, useful for establishing parent-child associations, managing grouped objects, or creating complex GameObject structures.
The positioning and movement of game elements are core to creating an interactive and engaging gaming experience. The 'Transform' class is therefore the lynchpin to most scripting operations in Unity. To explore more about 'Transform' and its effective use in Unity, the official Unity Transform documentation is a great learning material.
Properties
The 'childCount' provides you with the number of children the Transform has. It can be handy when you are dealing with GameObject hierarchies and need to manage multiple child GameObjects. Unity's Transform.childCount Documentation
The 'eulerAngles' property represents the rotation of the GameObject in degrees around the x, y, and z axes. The angles are measured in a specific order: z, x, and then y. Unity's Transform.eulerAngles Documentation
The 'forward' property corresponds to the blue axis of the GameObject and points in the direction of increasing z. This gives directions along the z-axis. Unity's Transform.forward Documentation
The 'hasChanged' property is a flag that indicates whether the transform's position or rotation has changed since the last frame. It’s handy for optimizing code as you can skip processing for static GameObjects. Remember to reset the flag manually after reading it. Unity's Transform.hasChanged Documentation
The 'hierarchyCapacity' property denotes the number of allocated transforms in the hierarchy data structure. Developers can adjust this capacity to optimize memory usage and performance. Unity's Transform.hierarchyCapacity Documentation
The 'hierarchyCount' property provides the current number of Transforms in the Transform's hierarchy data structure. This data may be used alongside 'hierarchyCapacity' to optimize game performance. Unity's Transform.hierarchyCount Documentation
The 'localEulerAngles' property provides a way to get or set the local rotation (relative to the parent GameObject) of the GameObject in terms of Euler angles. Unity's Transform.localEulerAngles Documentation
'localPosition' refers to the position of the GameObject relative to its parent GameObject. This property is useful when you want to arrange GameObjects in a hierarchical nested structure. Unity's Transform.localPosition Documentation
The 'localRotation' represents the rotation of the GameObject relative to the parent GameObject's rotation, as Quaternion. Unity's Transform.localRotation Documentation
The 'localScale' property enables you to get or set the scale of a GameObject relative to its parent GameObject. You can manipulate this property to resize GameObjects dynamically. Unity's Transform.localScale Documentation
The 'localToWorldMatrix' is the matrix that transforms a point from local space (i.e., relative to the object) into world space. Unity's Transform.localToWorldMatrix Documentation
The 'lossyScale' provides the global scale of the GameObject, which is the product of all parent scales combined with this GameObject’s local scale. It's a read-only value and gives the actual rendered scale of the GameObject. Unity's Transform.lossyScale Documentation
The 'parent' property provides a way to get or set the parent of this GameObject. It's particularly useful for managing GameObject hierarchies. Unity's Transform.parent Documentation
This property represents the position of the GameObject in world space coordinates. It can be used to get or set the current position of your GameObject. Unity's Transform.position Documentation
The 'right' property represents the red axis of the GameObject and points in the direction of increasing x. This gives directions along the x-axis. Unity's Transform.right Documentation
The 'root' property gives you the topmost transform in the hierarchy of the GameObject. Essentially, it retrieves the root GameObject that this GameObject is attached to. It's especially useful in large hierarchies. Unity's Transform.root Documentation
A Quaternion representing the orientation in space of the GameObject. Please note that for setting, it is better to use 'eulerAngles' as this property does not offer a degree of freedom. Unity's Transform.rotation Documentation
The 'up' property represents the green axis of the GameObject and it points in the direction of increasing y. In other words, this property provides directions along the y-axis. Unity's Transform.up Documentation
The 'worldToLocalMatrix' is a matrix that transforms a point from world space into local space (i.e., relative to the GameObject). Unity's Transform.worldToLocalMatrix Documentation
Methods
The 'DetachChildren' method unparents all the child GameObjects of the GameObject. This means those GameObjects will no longer receive any transformations from their parent. Unity's Transform.DetachChildren Documentation
Returns
The 'Find' method allows you to search for a child GameObject by its name. It is particularly useful when you need to access a specific child but do not have a reference to it. Be aware that this method is not optimal for regular use. If the child GameObject needs to be accessed frequently, it's less expensive to get a reference to that GameObject during initialization. Unity's Transform.Find Documentation
Parameters
n
The name of the child GameObject to find.
Returns
The transform of the found child GameObject.
The 'GetChild' method retrieves a child Transform by index. It can be tremendously useful when iterating over children in a parent-child hierarchy. Be aware that the Transform's sibling order also represents their order in the hierarchy. Unity's Transform.GetChild Documentation
Parameters
index
The index of the child Transform to retrieve.
Returns
The child Transform retrieved from the index.
The 'GetSiblingIndex' method retrieves the sibling index of the GameObject, helping to understand the order in which GameObjects are displayed. Unity's Transform.GetSiblingIndex Documentation
Returns
The current index of the GameObject.
The 'InverseTransformDirection' transforms a direction from world space to local space. This often becomes valuable when you want a world direction to be expressed relative to the local space of a GameObject. Unity's Transform.InverseTransformDirection Documentation
The 'InverseTransformPoint' transforms a position from world to local space. This method is handy when you want a globally defined position to be expressed relative to the local space of a GameObject. Unity's Transform.InverseTransformPoint Documentation
The 'InverseTransformVector' does the opposite of 'TransformVector'. It transforms a Vector from world space to local space. This method is primarily useful when you need a globally defined vector to be meaningful in the local space of a GameObject. Unity's Transform.InverseTransformVector Documentation
The 'IsChildOf' method checks if a transform is a child of a particular parent. It's a straightforward and useful way to identify relationships between GameObjects, like whether a specific GameObject is a part of a hierarchy. Unity's Transform.IsChildOf Documentation
Parameters
parent
The parent Transform to check against.
Returns
Returns true if the Transform is a child of parent, false otherwise.
The 'LookAt' method makes the GameObject rotate towards a target. This method is ideal in contexts where the GameObject needs to continually face another, for example, a GameObject camera following a character. Unity's Transform.LookAt Documentation
Parameters
target
Returns
The 'LookAt' method makes the GameObject rotate towards a target. This method is ideal in contexts where the GameObject needs to continually face another, for example, a GameObject camera following a character. Unity's Transform.LookAt Documentation
The 'LookAt' method makes the GameObject rotate towards a target. This method is ideal in contexts where the GameObject needs to continually face another, for example, a GameObject camera following a character. Unity's Transform.LookAt Documentation
Parameters
worldPosition
Returns
The 'LookAt' method makes the GameObject rotate towards a target. This method is ideal in contexts where the GameObject needs to continually face another, for example, a GameObject camera following a character. Unity's Transform.LookAt Documentation
Parameters
eulers
Returns
Parameters
axis
angle
Returns
Parameters
xAngle
yAngle
zAngle
Returns
Parameters
Returns
The 'RotateAround' method rotates the GameObject around a specific point by a given angle and along a specified axis. This method is quite useful when you want to make a GameObject revolve around a certain point. Unity's Transform.RotateAround Documentation
The 'SetAsFirstSibling' method moves the Transform to the beginning of the local transform list. This causes the GameObject to be rendered behind its siblings. Unity's Transform.SetAsFirstSibling Documentation
Returns
The 'SetAsLastSibling' method moves the Transform to the end of the local transform list. It results in the GameObject being rendered in front of its siblings. Unity's Transform.SetAsLastSibling Documentation
Returns
The 'SetLocalPositionAndRotation' method updates the local position and local rotation of the GameObject simultaneously. It's useful when you have colocated GameObjects and need to change their positions and rotations relative to their parent. Unity's Transform.SetLocalPositionAndRotation Documentation
Parameters
localPosition
The new local position for the GameObject.
localRotation
The new local rotation for the GameObject.
Returns
The 'SetParent' method is used to set the parent of the GameObject's transform. It's handy when you need to dynamically create parent-child relationships between GameObjects. Unity's Transform.SetParent Documentation
Parameters
The target parent Transform.
Returns
The 'SetParent' method is used to set the parent of the GameObject's transform. It's handy when you need to dynamically create parent-child relationships between GameObjects. Unity's Transform.SetParent Documentation
Parameters
parent
The target parent Transform.
worldPositionStays
Returns
The 'SetPositionAndRotation' method simultaneously changes the position and rotation of the GameObject. This method is more efficient than changing position and rotation separately. Unity's Transform.SetPositionAndRotation Documentation
Parameters
position
The new position for the GameObject.
rotation
The new rotation for the GameObject.
Returns
The 'SetSiblingIndex' method allows you to alter the sibling index. It changes the sibling order of the GameObject in the hierarchy and impacts the rendering order of GameObjects. Unity's Transform.SetSiblingIndex Documentation
Parameters
index
The new index for the GameObject.
Returns
The 'TransformDirection' method transforms a direction from local space to world space. This method can be useful when you want a locally defined direction to be meaningful in the world space. Unity's Transform.TransformDirection Documentation
The 'TransformPoint' transforms Position from local to world space. It takes into account the GameObject's position, rotation, and scale. Unity's Transform.TransformPoint Documentation
The 'TransformVector' method transforms a local Vector to world Vector. It considers the GameObject's rotation and scale in transforming the vector. Unity's Transform.TransformVector Documentation
Parameters
translation
Returns
Parameters
x
y
z
Returns
Parameters
Returns
Parameters
Returns
Creates a tween that moves the transform's local position from one position to the other.
Creates a tween that moves the transform's local position from one position to the other.
Creates a tween that moves the transform from its current local position to the specified local position.
Creates a tween that moves the transform's local rotation from one value to the other.
Parameters
Ending rotation of the object.
Returns
A Tween object you can use to further customize the motion.
Creates a tween that moves the transform's local rotation from one value to the other.
Parameters
from
Starting rotation of the object.
Ending rotation of the object.
Returns
A Tween object you can use to further customize the motion.
Creates a tween that moves the transform's local rotation from one value to the other.
Creates a tween that moves the transform's local rotation from one value to the other.
Creates a tween that moves the transform's local scale from one value to the other.
Creates a tween that moves the transform's local scale from one value to the other.
Creates a tween that moves the transform's local scale from one value to the other.
Parameters
to
Ending scale of the object.
Returns
A Tween object you can use to further customize the motion.
Creates a tween that moves the transform's local scale from one value to the other.
Parameters
from
Starting scale of the object.
to
Ending scale of the object.
Returns
A Tween object you can use to further customize the motion.
Creates a tween that changes the transform's local scale from its current scale to the specified scale.
Creates a tween that rotates an object to face a specific direction.
Creates a tween that rotates an object to face a specific direction.
Creates a tween that moves the transform from one position to the other.
Creates a tween that moves the transform from one position to the other.
Creates a tween that moves the transform from its current position to the specified position.
Creates a tween that moves the transform's rotation from one value to the other.
Parameters
Ending rotation of the object.
Returns
A Tween object you can use to further customize the motion.
Creates a tween that moves the transform's rotation from one value to the other.
Parameters
from
Starting rotation of the object.
Ending rotation of the object.
Returns
A Tween object you can use to further customize the motion.
Creates a tween that moves the transform's rotation from one value to the other.
Updated 19 days ago