Example #1
0
    public static float distanceToObject(this Vector2 position, GameObject obj)
    {
        Vector2       center = obj.getCollectiveColliderCenter();
        Vector2       dir    = (center - position).normalized;
        RaycastAnswer answer = Utility.RaycastAll(position, dir, (center - position).magnitude);

        for (int i = 0; i < answer.count; i++)
        {
            RaycastHit2D rch2d = answer.rch2ds[i];
            if (rch2d.collider.gameObject == obj)
            {
                return(rch2d.distance);
            }
        }
        throw new UnityException("Object " + obj + "'s raycast not found! This should not be possible!");
    }
    /// <summary>
    /// Determines whether the center of the first object has a direct line of sight to the center of the second object
    /// </summary>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <returns></returns>
    public static bool lineOfSight(this GameObject first, GameObject second)
    {
        Vector2       pos1   = first.transform.position;
        Vector2       pos2   = second.transform.position;
        RaycastAnswer answer = Utility.RaycastAll(pos1, pos2 - pos1, Vector2.Distance(pos1, pos2));

        for (int i = 0; i < answer.count; i++)
        {
            RaycastHit2D rch2d = answer.rch2ds[i];
            if (rch2d.collider.gameObject != first && rch2d.collider.gameObject != second)
            {
                return(false);
            }
        }
        return(true);
    }
Example #3
0
    /// <summary>
    /// Returns true if the two colliders overlap
    /// </summary>
    /// <param name=""></param>
    /// <param name="other"></param>
    /// <returns></returns>
    public static bool OverlapsCollider(this Collider2D coll, Collider2D other)
    {
        //If the bounds don't overlap
        if (!coll.bounds.Intersects(other.bounds))
        {
            //then these colliders definitely don't overlap
            return(false);
        }
        //Cast the first one to find if it has it
        RaycastAnswer answer = coll.CastAnswer(Vector2.zero, 0, false);

        for (int i = 0; i < answer.count; i++)
        {
            if (answer.rch2ds[i].collider == other)
            {
                return(true);
            }
        }
        return(false);
    }
Example #4
0
    /// <summary>
    /// Determines whether the center of the first object has a direct line of sight to the center of the second object
    /// </summary>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <returns></returns>
    public static bool lineOfSight(this GameObject first, GameObject second)
    {
        Vector2       pos1   = first.transform.position;
        Vector2       pos2   = second.transform.position;
        RaycastAnswer answer = Utility.RaycastAll(pos1, pos2 - pos1, Vector2.Distance(pos1, pos2));

        for (int i = 0; i < answer.count; i++)
        {
            RaycastHit2D rch2d = answer.rch2ds[i];
            if (rch2d.collider.isTrigger)
            {
                //don't process triggers
                continue;
            }
            GameObject collGO = rch2d.collider.gameObject;
            if (!collGO.equalsHierarchy(first) && !collGO.equalsHierarchy(second))
            {
                return(false);
            }
        }
        return(true);
    }