public override SurfaceDelimitation[] DetectSurface(Vector3 pos, Vector3 maxRayLength, float gridResolution, out Vector3 boundingBoxMin, out Vector3 boundingBoxMax) { // The surface is a rectangle defined by maxRayLengthX & maxRayLengthZ boundingBoxMin = pos - maxRayLength; boundingBoxMax = pos + maxRayLength; boundingBoxMax.y = pos.y; int LimitListLength = 1 + Mathf.CeilToInt((boundingBoxMax.x - boundingBoxMin.x) / gridResolution); SurfaceDelimitation[] surfaceLimits = new SurfaceDelimitation[LimitListLength]; for (int i = 0; i < LimitListLength; i++) { surfaceLimits[i].back = boundingBoxMin.z; surfaceLimits[i].front = boundingBoxMax.z; } return(surfaceLimits); }
/// <summary> /// Create 2 arrays for minZ/maxZ, which cover the XoZ surface, starting from StartX point, to StartX.x + lengthX. /// Rays are projected at every [resolution] interval along the X axis. /// </summary> /// <param name="StartX"></param>The starting point of the surface in X /// <param name="lengthX"></param>The X length of the surface /// <param name="maxZDistance"></param>The maximum distance to project rays in the Z axis /// <param name="resolution"></param>The resolution inbetween 2 rays along the X axis /// <param name="minZArray"></param>Output value; list of detected collisions toward Z- /// <param name="maxZArray"></param>Output value; list of detected collisions toward Z+ /// <returns></returns> SurfaceDelimitation[] ComputeZSurface(Vector3 StartX, float lengthX, float maxZDistance, float resolution) { int LimitListLength = 1 + Mathf.CeilToInt(lengthX / resolution); SurfaceDelimitation[] surface = new SurfaceDelimitation[LimitListLength]; Vector3 indexPos = StartX; for (int i = 1; i < LimitListLength - 1; i++) { indexPos.x = StartX.x + Mathf.Min((float)i * resolution, lengthX); RaycastHit hit; surface[i].back = CastRay(new Ray(indexPos, Vector3.back), maxZDistance, out hit) ? hit.point.z : indexPos.z - maxZDistance; surface[i].front = CastRay(new Ray(indexPos, Vector3.forward), maxZDistance, out hit) ? hit.point.z : indexPos.z + maxZDistance; } surface[0] = surface[1]; surface[LimitListLength - 1] = surface[LimitListLength - 2]; return(surface); }