Beispiel #1
0
    /// <summary>
    /// Gets a list of all environmental colliders hit given the following parameters.
    /// </summary>
    /// <param name="point1"></param>
    /// <param name="point2"></param>
    /// <param name="directionToCastRay"></param>
    /// <param name="distanceToCastRay"></param>
    /// <param name="totalPointsToCheck"></param>
    /// <returns></returns>
    private List <TileCollider> GetAllTilesHitFromRayCasts(Vector2 point1, Vector2 point2, Vector2 directionToCastRay, float distanceToCastRay, int totalPointsToCheck)
    {
        List <TileCollider> allCollidersThatWereHit = new List <TileCollider>();

        Vector2 segmentDistance = (point2 - point1) / (totalPointsToCheck - 1);

        Vector2 originPointForRaycast = point1;

        for (int i = 0; i < totalPointsToCheck; i++)
        {
            RaycastHit2D rayHit = Physics2D.Raycast(originPointForRaycast, directionToCastRay, distanceToCastRay, LayerMask.GetMask("Environment"));
            if (rayHit)
            {
                TileCollider tileColliderThatWasHit = rayHit.collider.GetComponent <TileCollider>();
                if (tileColliderThatWasHit)
                {
                    allCollidersThatWereHit.Add(tileColliderThatWasHit);
                }
            }

            DebugSettings.DrawLineDirection(originPointForRaycast, directionToCastRay, distanceToCastRay, Color.red);
            originPointForRaycast += segmentDistance;
        }

        return(allCollidersThatWereHit);
    }
 /// <summary>
 /// Gets a list of all colliders that intersect the line that passes through
 /// </summary>
 /// <param name="origin"></param>
 /// <param name="direction"></param>
 /// <param name="distance"></param>
 /// <param name="collidersHit"></param>
 /// <returns></returns>
 public bool CheckLineIntersectWithCollider(Vector2 origin, Vector2 direction, float distance, out List <CustomCollider2D> collidersHit)
 {
     collidersHit = new List <CustomCollider2D>();
     foreach (CustomCollider2D coll in nonStaticColliderList)
     {
         if (coll.enabled)
         {
             DebugSettings.DrawLineDirection(origin, direction, distance, Color.red);
             if (coll.LineIntersectWithCollider(origin, direction, distance))
             {
                 collidersHit.Add(coll);
             }
         }
     }
     return(collidersHit.Count >= 1);
 }