Created
February 14, 2025 10:33
-
-
Save Vavassor/acd222cb3189f643eafb04491d60eb5f to your computer and use it in GitHub Desktop.
Transform math without the Transform component in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| public static class TransformUtilities | |
| { | |
| /* | |
| Transforms represent a position, orientation (rotation), and scale of an object. They also | |
| represent a translation, rotation, and dilation relative to position (0, 0, 0), | |
| rotation (0, 0, 0, 1), and scale (1, 1, 1). | |
| When a transform is applied to another, it is translated, rotated, and dilated. It combines transformations. | |
| The inverse transform gets the position, orientation, and scale relative to another transform. It gets the change between transformations. | |
| Here's examples how to calculate transforms in different situations. | |
| Transforming using a Transform component. | |
| newPosition = transform.TransformPoint(position); | |
| newRotation = transform.rotation * rotation; | |
| newScale = transform.TransformVector(scale); | |
| Transforming using a position, rotation, and scale. | |
| newPosition = transformRotation * Vector3.Scale(position, transformScale) + transformPosition; | |
| newRotation = transformRotation * rotation; | |
| newScale = transformRotation * Vector3.Scale(scale, transformScale); | |
| Transforming using a position and rotation. | |
| newPosition = transformRotation * position + transformPosition; | |
| newRotation = transformRotation * rotation; | |
| Inverse transform using a Transform component. | |
| newPosition = transform.InverseTransformPoint(position); | |
| newRotation = Quaternion.Inverse(transform.rotation) * rotation; | |
| newScale = transform.InverseTransformVector(scale); | |
| Inverse transform using a position, rotation, and scale. | |
| var transformRotationInverse = Quaternion.Inverse(transformRotation); | |
| var inverseScale = new Vector3(1.0f / transformScale.x, 1.0f / transformScale.y, 1.0f / transformScale.z); | |
| newPosition = transformRotationInverse * Vector3.Scale(position - transformPosition, inverseScale); | |
| newRotation = transformRotationInverse * rotation; | |
| newScale = transformRotationInverse * Vector3.Scale(scale, inverseScale); | |
| Inverse transform using a position and rotation. | |
| var transformRotationInverse = Quaternion.Inverse(transformRotation); | |
| newPosition = transformRotationInverse * (position - transformPosition); | |
| newRotation = transformRotationInverse * rotation; | |
| Below are some functions that are more similar to Transform's public methods. | |
| https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Transform.html | |
| */ | |
| public static Vector3 InverseTransformDirection(Quaternion transformRotation, Vector3 direction) | |
| { | |
| return Quaternion.Inverse(transformRotation) * direction; | |
| } | |
| public static Vector3 InverseTransformPoint(Vector3 transformPosition, Quaternion transformRotation, Vector3 transformScale, Vector3 position) | |
| { | |
| var inverseScale = new Vector3(1.0f / transformScale.x, 1.0f / transformScale.y, 1.0f / transformScale.z); | |
| return Quaternion.Inverse(transformRotation) * Vector3.Scale(position - transformPosition, inverseScale); | |
| } | |
| public static Quaternion InverseTransformRotation(Quaternion transformRotation, Quaternion rotation) | |
| { | |
| return Quaternion.Inverse(transformRotation) * rotation; | |
| } | |
| public static Vector3 InverseTransformVector(Quaternion transformRotation, Vector3 transformScale, Vector3 scale) | |
| { | |
| return Quaternion.Inverse(transformRotation) * new Vector3(scale.x / transformScale.x, scale.y / transformScale.y, scale.z / transformScale.z); | |
| } | |
| public static Vector3 TransformDirection(Quaternion transformRotation, Vector3 direction) | |
| { | |
| return transformRotation * direction; | |
| } | |
| public static Vector3 TransformPoint(Vector3 transformPosition, Quaternion transformRotation, Vector3 transformScale, Vector3 position) | |
| { | |
| return transformRotation * Vector3.Scale(position, transformScale) + transformPosition; | |
| } | |
| public static Quaternion TransformRotation(Quaternion transformRotation, Quaternion rotation) | |
| { | |
| return transformRotation * rotation; | |
| } | |
| public static Vector3 TransformVector(Quaternion transformRotation, Vector3 transformScale, Vector3 scale) | |
| { | |
| return transformRotation * Vector3.Scale(scale, transformScale); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment