Created
August 8, 2015 07:17
-
-
Save FreyaHolmer/2583ae7f54e1a25b15c2 to your computer and use it in GitHub Desktop.
Adds WorldToLocal and LocalToWorld to transform components in Unity
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 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 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why didn't you use a
switch(..){..}
statement?