Skip to content

Instantly share code, notes, and snippets.

@beardordie
Created January 12, 2018 22:49
Show Gist options
  • Save beardordie/0d24c1e64e88f71b2167b336afd5ae78 to your computer and use it in GitHub Desktop.
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
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");
*/
@beardordie
Copy link
Author

A few more for transforms and Vector3s, with optional and named parameters, which results in good readability:

    // Usage:
    // transform.localPosition = transform.localPosition.With(z: 4f);
    public static Vector3 With(this Vector3 source, float? x=null, float? y = null, float? z = null)
    {
        float useX = x.HasValue ? (float)x : source.x;
        float useY = y.HasValue ? (float)y : source.y;
        float useZ = z.HasValue ? (float)z : source.z;
        return new Vector3(useX, useY, useZ);
    }

  // Usage:
    // transform.SetLocalPosition(z:4f);
    public static void SetLocalPosition(this Transform tr, float? x = null, float? y = null, float? z = null)
    {
        float useX = x.HasValue ? (float)x : tr.localPosition.x;
        float useY = y.HasValue ? (float)y : tr.localPosition.y;
        float useZ = z.HasValue ? (float)z : tr.localPosition.z;
        tr.localPosition = new Vector3(useX, useY, useZ);
    }

public static void SetWorldPosition(this Transform tr, float? x = null, float? y = null, float? z = null)
    {
        float useX = x.HasValue ? (float)x : tr.position.x;
        float useY = y.HasValue ? (float)y : tr.position.y;
        float useZ = z.HasValue ? (float)z : tr.position.z;
        tr.position = new Vector3(useX, useY, useZ);
    }

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