Class TransformExtensions
Provides extension methods for Unity's UnityEngine.Transform class, covering alternative coordinate system conversions (cylindrical, spherical, and polar), convenience reset and copy helpers, single-axis position setters, child management, deep hierarchy search, UnityEngine.RectTransform setup, and physics-component discovery.
Inherited Members
Namespace: Scylla.Core.Util
Assembly: ScyllaCore.dll
Syntax
public static class TransformExtensions
Remarks
Coordinate conventions used by this class:
-
Cylindrical
(r, theta, y):ris the radial distance in the XZ plane,thetais the azimuthal angle from the positive Z axis in radians, andyis the height along the world Y axis. -
Spherical
(rho, theta, phi):rhois the radial distance from the origin,thetais the azimuthal angle in the XZ plane from the positive Z axis (radians), andphiis the polar angle from the positive Y axis (radians). -
Polar
(r, theta): A 2D projection of cylindrical coordinates onto the XZ plane; the Y component of the world position is ignored on get and set to zero on set (unless a height overload is used).
All angle components are expressed in radians. Conversion math delegates to
MathUtil (CartesianToCylindrical, CylindricalToCartesian,
CartesianToSpherical, SphericalToCartesian, CartesianToPolar,
PolarToCartesian3D).
Methods
CopyFrom(Transform, Transform)
Copies the world-space position, world-space rotation, and local scale from
source into transform. Note that local scale
(not lossy world scale) is copied, so the result is only correct when both
transforms share the same parent hierarchy.
Declaration
public static void CopyFrom(this Transform transform, Transform source)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The destination transform that receives the copied values. |
| Transform | source | The transform to copy position, rotation, and local scale from. |
See Also
CopyLocalFrom(Transform, Transform)
Copies the local position, local rotation, and local scale from
source into transform. All three components
are relative to each transform's own parent, so this produces correct results only
when both transforms share the same parent or the intent is to mirror local space
values directly.
Declaration
public static void CopyLocalFrom(this Transform transform, Transform source)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The destination transform that receives the copied values. |
| Transform | source | The transform to copy local position, rotation, and scale from. |
See Also
DestroyChildren(Transform)
Destroys all immediate children of transform by calling
Destroy(Object, float) on each child's GameObject. Destruction is
deferred to the end of the current frame, as with all Object.Destroy calls.
Children are iterated in reverse index order to avoid index shifting during removal.
Declaration
public static void DestroyChildren(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The parent transform whose children are destroyed. |
Remarks
Use DestroyChildrenImmediate(Transform) when running in editor context or when
immediate destruction is required (e.g., inside OnValidate or edit-mode scripts).
See Also
DestroyChildrenImmediate(Transform)
Immediately destroys all children of transform by calling
DestroyImmediate(Object, bool) on each child's GameObject. Use this
variant in editor scripts (e.g., custom inspectors, OnValidate, or edit-mode
tooling) where deferred destruction is not processed. Children are iterated in
reverse index order to avoid index shifting during removal.
Declaration
public static void DestroyChildrenImmediate(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The parent transform whose children are destroyed immediately. |
Remarks
Do not call this method at runtime in Play Mode unless you specifically require synchronous object removal. DestroyChildren(Transform) is the preferred runtime alternative.
See Also
FindDeep(Transform, string)
Performs a depth-first recursive search of the entire child hierarchy rooted at
transform, including the root itself, and returns the first
UnityEngine.Transform whose name exactly matches name.
Declaration
public static Transform FindDeep(this Transform transform, string name)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The root of the hierarchy to search. The root itself is tested first; if its name matches, it is returned immediately without descending into children. |
| string | name | The exact |
Returns
| Type | Description |
|---|---|
| Transform | The first UnityEngine.Transform in the subtree whose name equals
|
Remarks
Unlike Unity's built-in Transform.Find, this method searches the full
subtree recursively rather than only immediate children. Performance is O(n) in
the number of transforms in the subtree.
FindPhysicsTransforms(Transform)
Recursively searches the entire child hierarchy of transform for
transforms that have a UnityEngine.Rigidbody or a UnityEngine.Collider component
(or both) attached. Returns a new list containing all matching transforms. Useful for
discovering ragdoll bones or physics proxy objects in a character hierarchy.
Declaration
public static List<Transform> FindPhysicsTransforms(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The root of the subtree to search. The root itself is not tested - only its descendants are evaluated. |
Returns
| Type | Description |
|---|---|
| List<Transform> | A new List<T> of UnityEngine.Transform objects that have a UnityEngine.Rigidbody or UnityEngine.Collider attached. The list is empty if no physics transforms are found. Order reflects depth-first traversal. |
See Also
FindPhysicsTransforms(Transform, List<Transform>)
Recursively searches the entire child hierarchy of transform for
transforms that have a UnityEngine.Rigidbody or UnityEngine.Collider component,
appending matches to the provided result list. Use this overload
when you want to avoid a list allocation or accumulate results from multiple roots
into a single shared list.
Declaration
public static void FindPhysicsTransforms(this Transform transform, List<Transform> result)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The root of the subtree to search. The root itself is not tested - only its descendants are evaluated. |
| List<Transform> | result | The list to which matching transforms are appended. If |
See Also
FindTransformsWithComponent<T>(Transform)
Recursively searches the entire child hierarchy of transform for
transforms that have a component of type T attached, and
returns a new list containing all matches.
Declaration
public static List<Transform> FindTransformsWithComponent<T>(this Transform transform) where T : Component
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The root of the subtree to search. The root itself is not tested - only its descendants are evaluated. |
Returns
| Type | Description |
|---|---|
| List<Transform> | A new List<T> of UnityEngine.Transform objects whose
|
Type Parameters
| Name | Description |
|---|---|
| T | The UnityEngine.Component type to search for. Only types assignable to
UnityEngine.Component are valid. The search uses
|
See Also
FindTransformsWithComponent<T>(Transform, List<Transform>)
Recursively searches the entire child hierarchy of transform for
transforms that have a component of type T attached, appending
matches to the provided result list. Use this overload to avoid
allocating a new list or to accumulate results from multiple hierarchy roots.
Declaration
public static void FindTransformsWithComponent<T>(this Transform transform, List<Transform> result) where T : Component
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The root of the subtree to search. Only descendants (not the root itself) are tested. |
| List<Transform> | result | The list to which matching transforms are appended. If |
Type Parameters
| Name | Description |
|---|---|
| T | The UnityEngine.Component type to search for. Identified via
|
See Also
GetCylindricalLocalPosition(Transform)
Returns the local position of transform relative to its parent,
expressed in cylindrical coordinates (r, theta, y).
Declaration
public static Vector3 GetCylindricalLocalPosition(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose local position is converted. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 in cylindrical form. See GetCylindricalPosition(Transform) for the component layout. |
See Also
GetCylindricalPosition(Transform)
Returns the world-space position of transform expressed in
cylindrical coordinates (r, theta, y), where r is the distance from
the world Y axis measured in the XZ plane, theta is the angle from the
positive Z axis in radians, and y is the world height.
Declaration
public static Vector3 GetCylindricalPosition(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose world position is converted. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 where |
See Also
GetPolarLocalPosition(Transform)
Returns the local position of transform relative to its parent
as 2D polar coordinates (r, theta) in the XZ plane. The local Y component
is discarded.
Declaration
public static Vector2 GetPolarLocalPosition(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose local position is projected and converted. |
Returns
| Type | Description |
|---|---|
| Vector2 | A UnityEngine.Vector2 where |
See Also
GetPolarPosition(Transform)
Returns the world-space position of transform as 2D polar
coordinates (r, theta) by projecting onto the XZ plane. The Y component
of the world position is discarded; use GetCylindricalPosition(Transform) if
you also need height.
Declaration
public static Vector2 GetPolarPosition(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose world position is projected and converted. |
Returns
| Type | Description |
|---|---|
| Vector2 | A UnityEngine.Vector2 where |
See Also
GetSphericalLocalPosition(Transform)
Returns the local position of transform relative to its parent,
expressed in spherical coordinates (rho, theta, phi).
Declaration
public static Vector3 GetSphericalLocalPosition(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose local position is converted. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 in spherical form. See GetSphericalPosition(Transform) for the component layout. |
See Also
GetSphericalPosition(Transform)
Returns the world-space position of transform expressed in
spherical coordinates (rho, theta, phi), where rho is the distance
from the world origin, theta is the azimuthal angle in the XZ plane from
the positive Z axis (radians), and phi is the polar angle measured down
from the positive Y axis (radians).
Declaration
public static Vector3 GetSphericalPosition(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose world position is converted. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 where |
See Also
Reset(Transform)
Resets all three local transform components to their identity values: local position to UnityEngine.Vector3.zero, local rotation to UnityEngine.Quaternion.identity, and local scale to UnityEngine.Vector3.one. Equivalent to calling ResetPosition(Transform), ResetRotation(Transform), and ResetScale(Transform) in sequence.
Declaration
public static void Reset(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to reset. |
See Also
ResetPosition(Transform)
Resets the local position of transform to
UnityEngine.Vector3.zero without affecting rotation or scale.
Declaration
public static void ResetPosition(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
See Also
ResetRotation(Transform)
Resets the local rotation of transform to
UnityEngine.Quaternion.identity without affecting position or scale.
Declaration
public static void ResetRotation(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
See Also
ResetScale(Transform)
Resets the local scale of transform to UnityEngine.Vector3.one
without affecting position or rotation.
Declaration
public static void ResetScale(this Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
See Also
SetCylindricalLocalPosition(Transform, Vector3)
Sets the local position of transform relative to its parent
from cylindrical coordinates (r, theta, y).
Declaration
public static void SetCylindricalLocalPosition(this Transform transform, Vector3 cylindrical)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose local position is set. |
| Vector3 | cylindrical | Cylindrical coordinates where |
See Also
SetCylindricalPosition(Transform, Vector3)
Sets the world-space position of transform from cylindrical
coordinates (r, theta, y). The input is converted to Cartesian coordinates
and assigned to transform.position.
Declaration
public static void SetCylindricalPosition(this Transform transform, Vector3 cylindrical)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose world position is set. |
| Vector3 | cylindrical | A UnityEngine.Vector3 representing cylindrical coordinates where |
See Also
SetLocalPositionX(Transform, float)
Sets only the X component of the local position of transform
relative to its parent, preserving the current local Y and Z values.
Declaration
public static void SetLocalPositionX(this Transform transform, float x)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
| float | x | The new local X coordinate. |
See Also
SetLocalPositionY(Transform, float)
Sets only the Y component of the local position of transform
relative to its parent, preserving the current local X and Z values.
Declaration
public static void SetLocalPositionY(this Transform transform, float y)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
| float | y | The new local Y coordinate. |
See Also
SetLocalPositionZ(Transform, float)
Sets only the Z component of the local position of transform
relative to its parent, preserving the current local X and Y values.
Declaration
public static void SetLocalPositionZ(this Transform transform, float z)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
| float | z | The new local Z coordinate. |
See Also
SetPolarLocalPosition(Transform, Vector2)
Sets the local position of transform relative to its parent from
2D polar coordinates (r, theta). The resulting local Y position is set to zero.
Use SetPolarLocalPosition(Transform, Vector2, float) to specify height.
Declaration
public static void SetPolarLocalPosition(this Transform transform, Vector2 polar)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose local position is set. |
| Vector2 | polar | A UnityEngine.Vector2 where |
See Also
SetPolarLocalPosition(Transform, Vector2, float)
Sets the local position of transform relative to its parent from
2D polar coordinates (r, theta) with an explicit local Y value.
Declaration
public static void SetPolarLocalPosition(this Transform transform, Vector2 polar, float height)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose local position is set. |
| Vector2 | polar | A UnityEngine.Vector2 where |
| float | height | The local-space Y coordinate to assign after the conversion, overriding zero. |
See Also
SetPolarPosition(Transform, Vector2)
Sets the world-space position of transform from 2D polar
coordinates (r, theta). The resulting world Y position is set to zero.
Use the SetPolarPosition(Transform, Vector2, float) overload to
preserve or specify an explicit height.
Declaration
public static void SetPolarPosition(this Transform transform, Vector2 polar)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose world position is set. |
| Vector2 | polar | A UnityEngine.Vector2 where |
See Also
SetPolarPosition(Transform, Vector2, float)
Sets the world-space position of transform from 2D polar
coordinates (r, theta) with an explicit world Y value.
Declaration
public static void SetPolarPosition(this Transform transform, Vector2 polar, float height)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose world position is set. |
| Vector2 | polar | A UnityEngine.Vector2 where |
| float | height | The world-space Y coordinate to assign after the polar-to-Cartesian conversion, overriding the default value of zero. |
See Also
SetPositionX(Transform, float)
Sets only the X component of the world-space position of transform,
preserving the current Y and Z values. Avoids the common boilerplate of reading,
modifying, and writing the position UnityEngine.Vector3.
Declaration
public static void SetPositionX(this Transform transform, float x)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
| float | x | The new world-space X coordinate. |
See Also
SetPositionY(Transform, float)
Sets only the Y component of the world-space position of transform,
preserving the current X and Z values.
Declaration
public static void SetPositionY(this Transform transform, float y)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
| float | y | The new world-space Y coordinate. |
See Also
SetPositionZ(Transform, float)
Sets only the Z component of the world-space position of transform,
preserving the current X and Y values.
Declaration
public static void SetPositionZ(this Transform transform, float z)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to modify. |
| float | z | The new world-space Z coordinate. |
See Also
SetSphericalLocalPosition(Transform, Vector3)
Sets the local position of transform relative to its parent
from spherical coordinates (rho, theta, phi).
Declaration
public static void SetSphericalLocalPosition(this Transform transform, Vector3 spherical)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose local position is set. |
| Vector3 | spherical | Spherical coordinates where |
See Also
SetSphericalPosition(Transform, Vector3)
Sets the world-space position of transform from spherical
coordinates (rho, theta, phi). The input is converted to Cartesian
coordinates and assigned to transform.position.
Declaration
public static void SetSphericalPosition(this Transform transform, Vector3 spherical)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform whose world position is set. |
| Vector3 | spherical | A UnityEngine.Vector3 representing spherical coordinates where |
See Also
SetupRectTransform(Transform, Vector2, float, float, float, float)
Gets or adds a UnityEngine.RectTransform component on transform's
GameObject and configures it for anchored positioning at a specific point
within its parent. Sets pivot, anchorMin, and anchorMax all
to anchor, then sets anchoredPosition to
(. Optionally applies an explicit
size if x, y)width or height is greater than zero.
Declaration
public static RectTransform SetupRectTransform(this Transform transform, Vector2 anchor, float x = 0, float y = 0, float width = 0, float height = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to configure. If a UnityEngine.RectTransform component does not already exist, one is added. |
| Vector2 | anchor | The normalized anchor point in the range [(0,0), (1,1)] that is applied to all
of |
| float | x | The anchored X position offset from the anchor point. Defaults to |
| float | y | The anchored Y position offset from the anchor point. Defaults to |
| float | width | If greater than zero, sets the element's width using
SetSizeWithCurrentAnchors(Axis, float). Defaults to |
| float | height | If greater than zero, sets the element's height using
SetSizeWithCurrentAnchors(Axis, float). Defaults to |
Returns
| Type | Description |
|---|---|
| RectTransform | The configured UnityEngine.RectTransform component. |
See Also
SetupRectTransformStretch(Transform, float, float, float, float)
Gets or adds a UnityEngine.RectTransform component on transform's
GameObject and configures it to stretch and fill its entire parent rect with
optional insets on each edge. Sets anchorMin to UnityEngine.Vector2.zero
and anchorMax to UnityEngine.Vector2.one, then applies edge offsets via
offsetMin and offsetMax.
Declaration
public static RectTransform SetupRectTransformStretch(this Transform transform, float left = 0, float right = 0, float top = 0, float bottom = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | transform | The transform to configure. If no UnityEngine.RectTransform component exists, one is added. |
| float | left | Inset from the parent's left edge in pixels. Positive values move the element's
left edge inward. Defaults to |
| float | right | Inset from the parent's right edge in pixels. Positive values move the element's
right edge inward. Defaults to |
| float | top | Inset from the parent's top edge in pixels. Positive values move the element's
top edge downward. Defaults to |
| float | bottom | Inset from the parent's bottom edge in pixels. Positive values move the element's
bottom edge upward. Defaults to |
Returns
| Type | Description |
|---|---|
| RectTransform | The configured UnityEngine.RectTransform component. |