Last active
December 2, 2022 21:46
-
-
Save ertanturan/54391656c4c8c7da3d636a0bc5225691 to your computer and use it in GitHub Desktop.
Scaled and Unscaled point transformations
This file contains 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 class PointTransformation{ | |
public static Vector3 TransformPoint(this Transform targetTransform, Vector3 vectorToChange) | |
{ | |
return targetTransform.localToWorldMatrix.MultiplyPoint3x4(vectorToChange); | |
} | |
public static Vector3 InverseTransformPoint(this Transform targetTransform, Vector3 vectorToInverse) | |
{ | |
return targetTransform.worldToLocalMatrix.MultiplyPoint3x4(vectorToInverse); | |
} | |
public static Vector3 TransformPointUnscaled(this Transform transform, Vector3 position) | |
{ | |
var localToWorldMatrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one); | |
return localToWorldMatrix.MultiplyPoint3x4(position); | |
} | |
public static Vector3 InverseTransformPointUnscaled(this Transform transform, Vector3 position) | |
{ | |
var worldToLocalMatrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one).inverse; | |
return worldToLocalMatrix.MultiplyPoint3x4(position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for anyone who wants to calculate global/local positions with or without bounding it to scale of the transform.