Skip to content

Instantly share code, notes, and snippets.

@FreyaHolmer
Created August 8, 2015 07:17
Show Gist options
  • Save FreyaHolmer/2583ae7f54e1a25b15c2 to your computer and use it in GitHub Desktop.
Save FreyaHolmer/2583ae7f54e1a25b15c2 to your computer and use it in GitHub Desktop.
Adds WorldToLocal and LocalToWorld to transform components in Unity
using UnityEngine;
public enum TransformType { Point, Direction, Vector };
public static class TransformExtensions {
public static Vector3 WorldToLocal( this Transform tf, Vector3 v3, TransformType type = TransformType.Point ) {
if( type == TransformType.Point )
return tf.InverseTransformPoint( v3 );
else if( type == TransformType.Direction )
return tf.InverseTransformDirection( v3 );
else
return tf.InverseTransformVector( v3 );
}
public static Vector3 LocalToWorld( this Transform tf, Vector3 v3, TransformType type = TransformType.Point ) {
if( type == TransformType.Point )
return tf.TransformPoint( v3 );
else if( type == TransformType.Direction )
return tf.TransformDirection( v3 );
else
return tf.TransformVector( v3 );
}
}
@petereichinger
Copy link

Why didn't you use a switch(..){..} statement?

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