Ejemplo n.º 1
0
        public bool IsColliding(Transform casterTransform, bool flipDirection, string maskName = "Raycast")
        {
            bool hit = GameCorePhysics2D.HasHit(CastRays(casterTransform, flipDirection, maskName));

            if (useEvent && hit)
            {
                CollisionEvent.Raise();
            }

            return(hit);
        }
Ejemplo n.º 2
0
        public RaycastHit2D DetectCollisionRaycast(Transform casterTransform, bool flipDirection, string maskName = "Raycast")
        {
            RaycastHit2D hit = CastRays(casterTransform, flipDirection, maskName);

            if (useEvent && GameCorePhysics2D.HasHit(hit))
            {
                CollisionEvent.Raise();
            }

            return(hit);
        }
Ejemplo n.º 3
0
        private RaycastHit2D CastRays(Transform casterTransform, bool flipDirection, string maskName)
        {
            float        nearestHitDistance = Mathf.Infinity;
            RaycastHit2D outputHit          = new RaycastHit2D();

            for (int i = 0; i < CollisionDetectionRays.Count; i++)
            {
                RayPackage2D ray;
                if (flipDirection)
                {
                    ray = CollisionDetectionRays[i].FlipAcrossDirectionAxis();
                }
                else
                {
                    ray = CollisionDetectionRays[i];
                }

                RaycastHit2D hit = GameCorePhysics2D.RayCast(
                    (Vector2)casterTransform.position + ray.OriginPosition,
                    ray.Direction,
                    ray.Distance,
                    LayerMask.GetMask(maskName),
                    CollisionTags
                    );

                // TODO: Use layers instead of tags? or use RayCast all?

#if UNITY_EDITOR
                Debug.DrawRay(
                    (Vector2)casterTransform.position + ray.OriginPosition,
                    ray.Direction * ray.Distance,
                    Color.red
                    );
#endif

                // If the hit is legit, and it is the shortest hit, make it returnable
                //    All casts are important to prevent biases based on the order of raycasting.
                if (GameCorePhysics2D.HasHit(hit) && hit.distance < nearestHitDistance)
                {
                    nearestHitDistance = hit.distance;
                    outputHit          = hit;
                }
            }

            return(outputHit);
        }