private void CheckSurroundingDirections() { if (DebugMode) { ClearSurroundingCornerObjects(); } surroundingCornersPoints = new List <Vector3>(); var startDirection = transform.forward; var direction = startDirection; var position = transform.position; var directionCheckResult = new DirectionCheckResult { IsEmpty = true }; var iterations = Mathf.CeilToInt(360f / sweepAngleStep); for (int i = 0; i < iterations; i++) { directionCheckResult.intermediatePointStepCount++; directionCheckResult = CheckGivenDirection(direction, position, directionCheckResult); direction = Quaternion.Euler(0, sweepAngleStep, 0) * direction; } }
private DirectionCheckResult CheckGivenDirection(Vector3 direction, Vector3 position, DirectionCheckResult lastResult) { var intermediatePointStepCount = lastResult.intermediatePointStepCount; var lastHittedObject = lastResult.lastHittedObject; var lastHittedPoint = lastResult.lastHittedPoint; GameObject hitedObject = null; Vector3 hitedPoint = Vector3.zero; var hitList = Physics.RaycastAll(position, direction, detectionDistanceLimit); Array.Sort(hitList, new RaycastHitSorting(position)); foreach (var hit in hitList) { if (IsATargetObject(hit.transform.gameObject)) { var obj = hit.transform.gameObject; hitedObject = obj; hitedPoint = hit.point; intermediatePointStepCount = 0; break; } } if (!lastResult.IsEmpty && hitedObject != lastHittedObject) { if (lastHittedObject != null) { surroundingCornersPoints.Add(lastHittedPoint); if (DebugMode) { surroundingCornerObjects.Add(Instantiate(detectedPointPrefab, lastHittedPoint, new Quaternion())); } } if (hitedObject != null) { surroundingCornersPoints.Add(hitedPoint); if (DebugMode) { surroundingCornerObjects.Add(Instantiate(detectedPointPrefab, hitedPoint, new Quaternion())); } } } else if (intermediatePointStepCount == intermediatePointStep) { var intermediatePos = transform.position + direction * detectionDistanceLimit; surroundingCornersPoints.Add(intermediatePos); intermediatePointStepCount = 0; if (DebugMode) { surroundingCornerObjects.Add(Instantiate(intermediatePointPrefab, intermediatePos, new Quaternion())); } } lastResult.IsEmpty = false; lastResult.lastHittedObject = hitedObject; lastResult.lastHittedPoint = hitedPoint; lastResult.intermediatePointStepCount = intermediatePointStepCount; return(lastResult); }