protected void ClearColliders() { var collidersEnumerator = _gameObjectColliders.GetEnumerator(); try { while (collidersEnumerator.MoveNext()) { var colliderList = collidersEnumerator.Current.Value; ColliderListCache.Dispose(colliderList); } } finally { collidersEnumerator.Dispose(); } _gameObjectColliders.Clear(); collidersEnumerator = _rigidBodyColliders.GetEnumerator(); try { while (collidersEnumerator.MoveNext()) { var colliderList = collidersEnumerator.Current.Value; ColliderListCache.Dispose(colliderList); } } finally { collidersEnumerator.Dispose(); } _rigidBodyColliders.Clear(); DetectedColliders.Clear(); ClearLineOfSight(); }
// OnTriggerExit is called when the Collider other has stopped touching the trigger private void OnTriggerExit(Collider other) { // Remove detected object when leaving if (detectedColliders.Contains(other)) { DetectedColliders.Remove(other); // Do some actions if the nearest object gets removed if (nearestCollider == other) { // Disable outline on remove from list if (nearestCollider.gameObject.HasTag("Outline")) { nearestCollider.GetComponentInChildren <TDS_DiffuseOutline>().DisableOutline(); } } // Desactivate feedback if needed if (detectedColliders.Count == 0) { nearestCollider = null; // Desactivate feedback if (interactText.gameObject.activeInHierarchy) { interactText.gameObject.SetActive(false); } } } }
// OnTriggerEnter is called when the GameObject collides with another GameObject private void OnTriggerEnter(Collider other) { // If detected object has matching tag, add it if (other.gameObject.HasTag(DetectedTags.ObjectTags) && !DetectedColliders.Contains(other)) { DetectedColliders.Add(other); } }
protected virtual void OnEnable() { _rigidBodyColliders.Clear(); _gameObjectColliders.Clear(); _rayCastTargets.Clear(); _objectVisibility.Clear(); _raycastResults.Clear(); _tempGoList.Clear(); DetectedColliders.Clear(); }
protected virtual GameObject RemoveCollider(Collider c) { if (c == null) { ClearDestroyedGameObjects(); return(null); } GameObject colliderDetectionLost = null; GameObject rigidBodyDetectionLost = null; if (RemoveColliderFromMap(c, c.gameObject, _gameObjectColliders)) { DisposeRayCastTarget(c.gameObject); colliderDetectionLost = c.gameObject; } if (c.attachedRigidbody != null && RemoveColliderFromMap(c, c.attachedRigidbody.gameObject, _rigidBodyColliders)) { DisposeRayCastTarget(c.attachedRigidbody.gameObject); rigidBodyDetectionLost = c.attachedRigidbody.gameObject; } var detectionLost = DetectionMode == SensorMode.Colliders ? colliderDetectionLost : rigidBodyDetectionLost; if (ShouldIgnore(detectionLost)) { return(null); } DetectedColliders.Remove(c); if (RequiresLineOfSight && detectionLost != null) { if (_objectVisibility.ContainsKey(detectionLost)) { _objectVisibility.Remove(detectionLost); return(detectionLost); } return(null); } if (detectionLost != null) { _objectVisibility.Remove(detectionLost); } return(detectionLost); }
protected virtual GameObject AddCollider(Collider c) { GameObject newColliderDetection = null; GameObject newRigidBodyDetection = null; if (AddColliderToMap(c, c.gameObject, _gameObjectColliders)) { DisposeRayCastTarget(c.gameObject); newColliderDetection = c.gameObject; } if (c.attachedRigidbody != null && AddColliderToMap(c, c.attachedRigidbody.gameObject, _rigidBodyColliders)) { DisposeRayCastTarget(c.attachedRigidbody.gameObject); newRigidBodyDetection = c.attachedRigidbody.gameObject; } var newDetection = DetectionMode == SensorMode.Colliders ? newColliderDetection : newRigidBodyDetection; if (ShouldIgnore(newDetection)) { return(null); } if (RequiresLineOfSight && newDetection != null) { bool prevDetected = _objectVisibility.ContainsKey(newDetection) && _objectVisibility[newDetection] >= _minimumVisibility; _objectVisibility[newDetection] = TestObjectVisibility(newDetection); if (!prevDetected && _objectVisibility[newDetection] >= _minimumVisibility) { if (!DetectedColliders.Contains(c)) { DetectedColliders.Add(c); } return(newDetection); } return(null); } if (!DetectedColliders.Contains(c)) { DetectedColliders.Add(c); } return(newDetection); }
/// <summary> /// Clears colliders from the detected colliders list that are disables. /// </summary> private void OrderObjects() { // If not enabled or not detecting anything, return if (detectedColliders.Count == 0) { // Desactivate feedback if needed if (interactText.gameObject.activeInHierarchy) { interactText.gameObject.SetActive(false); } return; } // Get element(s) to remove Collider[] _toRemove = new List <Collider>(detectedColliders).Where(d => (d == null) || (d.enabled == false)).ToArray(); if (_toRemove.Length > 0) { // Remove all disable colliders foreach (Collider _collider in _toRemove) { DetectedColliders.Remove(_collider); // Do some actions if the nearest object gets removed from list if (nearestCollider == _collider) { // Disable outline on remove from list if ((_collider != null) && nearestCollider.gameObject.HasTag("Outline")) { nearestCollider.GetComponentInChildren <TDS_DiffuseOutline>().DisableOutline(); } } } } // Order objects if (detectedColliders.Count > 0) { Vector2 _pos = new Vector2(transform.position.x, transform.position.z); detectedColliders = detectedColliders.OrderBy(c => Mathf.Abs((_pos - new Vector2(c.transform.position.x, c.transform.position.z)).magnitude)).ToList(); // Set nearest collider if different if (nearestCollider != detectedColliders[0]) { // Disable outline if (nearestCollider && nearestCollider.gameObject.HasTag("Outline")) { nearestCollider.GetComponentInChildren <TDS_DiffuseOutline>().DisableOutline(); } nearestCollider = detectedColliders[0]; // If feedback should be displayed, display it if (doDisplayFeedback && !interactText.gameObject.activeInHierarchy) { interactText.gameObject.SetActive(true); } // If the object has outline tag, activate the outline on it if (doDisplayFeedback && nearestCollider.gameObject.HasTag("Outline")) { nearestCollider.GetComponentInChildren <TDS_DiffuseOutline>().EnableOutline(); } } } // Desactivate feedback if needed else if (interactText.gameObject.activeInHierarchy) { interactText.gameObject.SetActive(false); } }