Transform
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
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. For more details, refer to the official Unity 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. Additional details can be found in the official Unity 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. Refer to the official Unity documentation to learn more.
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. More details can be found in the official Unity 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. Additional details in the official Unity documentation can provide more contextual understanding.
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. More information can be found in the official Unity 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. Additional details can be found in the official Unity 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. Further information can be found in the official Unity documentation.
The 'localRotation' represents the rotation of the GameObject relative to the parent GameObject's rotation, as Quaternion. Further details can be found in the official Unity 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. For more information, see the official Unity documentation.
The 'parent' property provides a way to get or set the parent of this GameObject. It's particularly useful for managing GameObject hierarchies. Check out the official Unity documentation for more information.
The 'worldToLocalMatrix' is a matrix that transforms a point from world space into local space (i.e., relative to the GameObject). To understand more about this property, refer to the Unity Documentation.
The 'localToWorldMatrix' is the matrix that transforms a point from local space (i.e., relative to the object) into world space. More context can be found in the Unity 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. Refer to the Unity documentation for more details.
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. More details in the Unity 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. To understand its use, check the Unity 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. Check out the Unity documentation for more details.
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. Visit the Unity Documentation for more details.
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. To understand more, visit the Unity Documentation.
Methods
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. For more details, refer to the official Unity documentation.
Parameters
targetPos
Vector3Returns
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. For further understanding, refer to the official Unity Documentation.
Parameters
The target parent Transform.
Returns
The 'SetPositionAndRotation' method simultaneously changes the position and rotation of the GameObject. This method is more efficient than changing position and rotation separately. For more details, check out the official Unity documentation.
Parameters
position
Vector3The new position for the GameObject.
rotation
QuaternionThe new rotation for the GameObject.
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. Visit Unity's official documentation for more details.
Parameters
localPosition
Vector3The new local position for the GameObject.
localRotation
QuaternionThe new local rotation for the GameObject.
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. For better understanding, check the official Unity documentation.
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. More information can be found in the official Unity documentation.
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. Check out the official Unity documentation for more insights.
The 'TransformVector' method transforms a local Vector to world Vector. It considers the GameObject's rotation and scale in transforming the vector. For additional context, see the official Unity 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. For more context, refer to the official Unity documentation.
The 'TransformPoint' transforms Position from local to world space. It takes into account the GameObject's position, rotation, and scale. More information about how to use this method can be found in the official Unity 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. Refer to the official Unity documentation to understand more about its functionality.
The 'DetachChildren' method unparents all the child GameObjects of the GameObject. This means those GameObjects will no longer receive any transformations from their parent. Check out the official Unity Documentation for more context.
Returns
The 'SetAsFirstSibling' method moves the Transform to the beginning of the local transform list. This causes the GameObject to be rendered behind its siblings. Further details can be found in the Unity 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. More information can be found in the official Unity documentation.
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. You can find more details on the official Unity documentation.
Parameters
index
The new index for the GameObject.
Returns
The 'GetSiblingIndex' method retrieves the sibling index of the GameObject, helping to understand the order in which GameObjects are displayed. More contextual insights can be found in the official Unity Documentation.
Returns
The current index of the GameObject.
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. Check out the official Unity Documentation to learn more.
Parameters
n
The name of the child GameObject to find.
Returns
The transform of the found child GameObject.
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. More details can be found in the official Unity documentation.
Parameters
parent
TransformThe parent Transform to check against.
Returns
Returns true if the Transform is a child of parent, false otherwise.
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. For more context, refer to the official Unity documentation.
Parameters
index
The index of the child Transform to retrieve.
Returns
The child Transform retrieved from the index.