Created
January 12, 2018 22:49
-
-
Save beardordie/0d24c1e64e88f71b2167b336afd5ae78 to your computer and use it in GitHub Desktop.
Unity GameObject Extensions for human-readable code when working with 2D transform positions
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
public static class My2DExtensions { | |
public static bool IsLeftOf(this GameObject t1, GameObject t2) | |
{ | |
if(t1.transform.position.x < t2.transform.position.x){ return true; } else { return false; }) | |
} | |
public static bool IsRightOf(this GameObject t1, GameObject t2) | |
{ | |
if(t1.transform.position.x < t2.transform.position.x){ return true; } else { return false; } | |
} | |
public static bool IsCenterOf(this GameObject t1, GameObject t2, float wiggleRoom=0f) | |
{ | |
if(t1.transform.position.Distance(new Vector2(t2.transform.position.x, t1.transform.position.y))<=wiggleRoom) return true; else return false; | |
} | |
public static bool IsAbove(this GameObject t1, GameObject t2) | |
{ | |
if(t1.transform.position.y > t2.transform.position.y){ return true; } else { return false; } | |
} | |
public static bool IsBelow(this GameObject t1, GameObject t2) | |
{ | |
if(t1.transform.position.y < t2.transform.position.y){ return true; } else { return false; } | |
} | |
public static bool IsMiddleOf(this GameObject t1, GameObject t2, float wiggleRoom=0f) | |
{ | |
if(t1.transform.position.Distance(new Vector2(t1.transform.position.x, t2.transform.position.y))<=wiggleRoom) return true; else return false; | |
} | |
} | |
/* Usage in another script: | |
if(thing1.IsLeftOf(thing2)) Debug.Log("It's to the left"); | |
if(thing1.IsAbove(thing2)) Debug.Log("It's above"); | |
if(thing1.IsCenterOf(thing2, 0.5f)) Debug.Log("It's centered within 0.5f horizontal distance"); | |
if(thing1.IsMiddleOf(thing2, 0.5f)) Debug.Log("It's centered within 0.5f vertical distance"); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few more for transforms and Vector3s, with optional and named parameters, which results in good readability: