Esempio n. 1
0
        public void Sample(Vector2 position, float distance, LayerMask mask)
        {
            var   hits            = new RaycastHit2D[Rays.Length];
            bool  hittedAnything  = false;
            float closestDistance = distance;

            bool lastQueriesHitTriggers      = Physics2D.queriesHitTriggers;
            bool lastQueriesStartInColliders = Physics2D.queriesStartInColliders;

            Physics2D.queriesHitTriggers = false;

            // TODO: Find better way to handle "my own collider" raycasting
            Physics2D.queriesStartInColliders = /*false*/ true;

            for (int i = 0; i < Rays.Length; i++)
            {
                RayData ray = Rays[i];
                hits[i] = Physics2D.Raycast(position + ray.Offset, ray.Direction, distance, mask);

                if (hits[i].transform != null)
                {
                    hittedAnything  = true;
                    closestDistance = Mathf.Min(closestDistance, hits[i].distance);
                }
                //Debug.DrawRay(position + ray.Offset, ray.Direction * distance, _debugColor);
            }

            Physics2D.queriesHitTriggers      = lastQueriesHitTriggers;
            Physics2D.queriesStartInColliders = lastQueriesStartInColliders;

            Hit.Set(hittedAnything, closestDistance);
        }
        public RaycasterHit Sample(Vector2 position, float distance, LayerMask mask)
        {
            var   hits           = new RaycastHit2D[rays.Length];
            bool  hittedAnything = false;
            float maxDist        = 0;

            for (int i = 0; i < rays.Length; i++)
            {
                RayData ray = rays[i];
                hits[i]        = Physics2D.Raycast(position + ray.offset, ray.direction, distance, mask);
                hittedAnything = hittedAnything || hits[i].transform != null;
                maxDist        = Mathf.Max(maxDist, hits[i].distance);
            }

            return(new RaycasterHit(hits, hittedAnything, maxDist));
        }
        public RaycastArray(int raysPerSide, float width, Vector3 direction, Vector3 baseOffset = new Vector3())
        {
            if (raysPerSide < 1)
            {
                throw new System.ArgumentOutOfRangeException("Rays per size needs to be higher or equal to 1");
            }
            rays = new RayData[(raysPerSide * 2) + 1];

            float   spacing    = (width * 0.5f) / raysPerSide;
            Vector3 perpVector = Quaternion.Euler(0, 0, 90) * direction;


            rays[0] = new RayData(baseOffset, direction);
            for (int i = 0; i < raysPerSide; i++)
            {
                rays[((i + 1) * 2) - 1] = new RayData(baseOffset + perpVector * spacing * (i + 1), direction);
                rays[((i + 1) * 2)]     = new RayData(baseOffset - perpVector * spacing * (i + 1), direction);
            }
        }
Esempio n. 4
0
        private static RayData[] GetRays(int raysPerSide, float width, Vector3 direction, Vector3 baseOffset)
        {
            if (raysPerSide < 1)
            {
                throw new System.ArgumentOutOfRangeException("Rays per side needs to be higher or equal to 1");
            }
            RayData[] rays = new RayData[(raysPerSide * 2) + 1];

            float   spacing             = (width * 0.5f) / raysPerSide;
            Vector3 perpendicularVector = Quaternion.Euler(0, 0, 90) * direction;

            rays[0] = new RayData(baseOffset, direction);
            for (int i = 0; i < raysPerSide; i++)
            {
                int n = ((i + 1) * 2);
                rays[n]     = new RayData(baseOffset + perpendicularVector * spacing * (i + 1), direction);
                rays[n - 1] = new RayData(baseOffset - perpendicularVector * spacing * (i + 1), direction);
            }

            return(rays);
        }