public static List <RaycastResultHelper> All(Vector3 origin, Vector3 direction, float distance, LayerMask layerMask, List <Collider> movedColliders) { // Check to see if the ray cast hits any of the requested Unity layers List <RaycastResultHelper> results = null; if (layerMask != 0) { var raycastHits = UnityEngine.Physics.RaycastAll(origin, direction, distance, layerMask); if (raycastHits != null) { results = new List <RaycastResultHelper>(raycastHits.Length); foreach (var raycastHit in raycastHits) { results.Add(new RaycastResultHelper(raycastHit, layerMask)); } } } // If we have colliders that have moved, then remove them from the results list and redo the raycast. if (movedColliders != null) { RaycastHit hitInfo; Ray ray = new Ray(origin, direction); foreach (var collider in movedColliders) { if ((collider.gameObject.layer & layerMask) != 0) { int colliderIndex = results.FindIndex(x => x.Collider == collider); if (collider.Raycast(ray, out hitInfo, distance)) { if (colliderIndex >= 0) { results[colliderIndex] = new RaycastResultHelper(hitInfo, layerMask); } else { results.Add(new RaycastResultHelper(hitInfo, layerMask)); } } else if (colliderIndex >= 0) { results.RemoveAt(colliderIndex); } } } } // Unity doesn't return hit results in any particular order, so we need to sort them to closest first. if (results != null) { results.Sort((x, y) => x.Distance < y.Distance ? -1 : 1); } return(results); }
public static bool SphereFirst(Vector3 origin, Vector3 direction, float radius, float distance, LayerMask layerMask, out RaycastResultHelper result) { result = default(RaycastResultHelper); RaycastHit hit; bool hitSomething = false; // Check to see if the ray cast hits any of the requested Unity layers if (layerMask != 0 && UnityEngine.Physics.SphereCast(origin, radius, direction, out hit, distance, layerMask)) { result = new RaycastResultHelper(hit, layerMask); hitSomething = true; } return(hitSomething); }
private static bool DefaultSpherecast(Vector3 origin, Vector3 direction, float radius, float distance, LayerMask surface, out RaycastResultHelper result) { return(RaycastHelper.SphereFirst(origin, direction, radius, distance, surface, out result)); }