Skip to content

Instantly share code, notes, and snippets.

@ertanturan
Last active December 2, 2022 21:46
Show Gist options
  • Save ertanturan/54391656c4c8c7da3d636a0bc5225691 to your computer and use it in GitHub Desktop.
Save ertanturan/54391656c4c8c7da3d636a0bc5225691 to your computer and use it in GitHub Desktop.
Scaled and Unscaled point transformations
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);
}
}
@ertanturan
Copy link
Author

for anyone who wants to calculate global/local positions with or without bounding it to scale of the transform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment