5 using System.Collections.Generic;
12 public static bool DebugEnabled =
false;
14 public delegate
bool RaycastFunc(Vector3 origin, Vector3 direction,
float distance, LayerMask layerMask, out
RaycastResultHelper result);
16 public static bool First(Vector3 origin, Vector3 direction,
float distance, LayerMask layerMask, out
RaycastResultHelper result)
21 bool hitSomething =
false;
25 UnityEngine.Physics.Raycast(origin, direction, out hit, distance, layerMask))
40 bool hitSomething =
false;
44 UnityEngine.Physics.SphereCast(origin, radius, direction, out hit, distance, layerMask))
54 public static List<RaycastResultHelper>
All(Vector3 origin, Vector3 direction,
float distance, LayerMask layerMask)
56 return All(origin, direction, distance, layerMask, null);
59 public static List<RaycastResultHelper>
All(Vector3 origin, Vector3 direction,
float distance, LayerMask layerMask, List<Collider> movedColliders)
62 List<RaycastResultHelper> results = null;
65 var raycastHits =
UnityEngine.Physics.RaycastAll(origin, direction, distance, layerMask);
66 if (raycastHits != null)
68 results =
new List<RaycastResultHelper>(raycastHits.Length);
69 foreach (var raycastHit
in raycastHits)
77 if (movedColliders != null)
80 Ray ray =
new Ray(origin, direction);
81 foreach (var collider
in movedColliders)
83 if ((collider.gameObject.layer & layerMask) != 0)
85 int colliderIndex = results.FindIndex(x => x.Collider == collider);
87 if (collider.Raycast(ray, out hitInfo, distance))
89 if (colliderIndex >= 0)
98 else if (colliderIndex >= 0)
100 results.RemoveAt(colliderIndex);
109 results.Sort((x, y) => x.Distance < y.Distance ? -1 : 1);
117 return boxCollider.size;
121 public static bool CastBoxExtents(Vector3 extents, Vector3 targetPosition, Matrix4x4 trs, Ray ray,
float maxDistance, LayerMask surface, RaycastFunc raycastFunc,
int raysPerEdge,
bool ortho, out Vector3[] points, out Vector3[] normals, out
bool[] hits)
123 bool debugEnabled = DebugEnabled;
126 Debug.DrawLine(ray.origin, ray.origin + ray.direction * 10.0f, Color.green);
129 extents /= (raysPerEdge - 1);
131 int halfRaysPerEdge = (raysPerEdge - 1) / 2;
132 int numRays = raysPerEdge * raysPerEdge;
134 bool hitSomething =
false;
136 points =
new Vector3[numRays];
137 normals =
new Vector3[numRays];
138 hits =
new bool[numRays];
142 for (
int x = -halfRaysPerEdge; x <= halfRaysPerEdge; x += 1)
144 for (
int y = -halfRaysPerEdge; y <= halfRaysPerEdge; y += 1)
146 Vector3 offset = trs.MultiplyVector(
new Vector3(x * extents.x, y * extents.y, 0));
148 Vector3 origin = ray.origin;
149 Vector3 direction = (targetPosition + offset) - ray.origin;
154 direction = ray.direction;
158 hits[index] = raycastFunc(origin, direction, maxDistance, surface, out rayHit);
163 points[index] = rayHit.Point;
164 normals[index] = rayHit.Normal;
168 Debug.DrawLine(origin, points[index], Color.yellow);
175 Debug.DrawLine(origin, origin + direction * 3.0f, Color.gray);