private void CheckSurroundings() { Collider[] hit = Physics.OverlapSphere(_rb.position, Radius); GameObjectsInSurroundings.Clear(); GameObjectsInView.Clear(); for (int i = 0; i < hit.Length; i++) { var collider = hit[i]; int gameObjectLayer = collider.gameObject.layer; int layerMask = (gameObjectLayer > 0) ? 1 << gameObjectLayer : gameObjectLayer; bool isInLayerMask = (mask.value & layerMask) == layerMask; if (!GameObjectsInSurroundings.Contains(collider.gameObject) && isInLayerMask && collider.gameObject != this.gameObject) { GameObjectsInSurroundings.Add(collider.gameObject); } } foreach (var item in GameObjectsInSurroundings) { if (IsInView(item)) { if (!GameObjectsInView.Contains(item)) { GameObjectsInView.Add(item); } } else { if (GameObjectsInView.Contains(item)) { GameObjectsInView.Remove(item); } } } }
private void OnDrawGizmos() { if (enableDraw) { Gizmos.color = Color.blue; if (_rb != null) { var rightSide = TransformVector(_rb.transform.forward * Radius, Angle) + _rb.position; var leftSide = TransformVector(_rb.transform.forward * Radius, -Angle) + _rb.position; rightSide.y = _rb.position.y; leftSide.y = _rb.position.y; //draw field of vision Gizmos.DrawLine(_rb.position, rightSide); Gizmos.DrawLine(_rb.position, leftSide); Gizmos.DrawWireSphere(_rb.position, Radius); Gizmos.color = Color.cyan; Gizmos.DrawLine(_rb.position, (_rb.transform.forward * Radius) + _rb.position); //draw a line to each item seen foreach (var item in GameObjectsInSurroundings) { Color color = Color.red; if (GameObjectsInView.Contains(item)) { color = Color.green; } Gizmos.color = color; Gizmos.DrawLine(_rb.position, item.transform.position); } } } }