/// <summary> /// Check if any target was hit by this object /// </summary> /// <returns>Returns the ID of the elements hit with their damages</returns> public Dictionary <int, int> CheckHit() { int _damages = 0; bool _hasHitEnemy = false; bool _hasHitGround = false; TDS_DamageableElement _target = null; Dictionary <int, int> _hitElements = new Dictionary <int, int>(); // Get all colliders in the range of the object's attack box Collider[] _touchColliders = Physics.OverlapBox(transform.TransformPoint(attackBox.Collider.center), Vector3.Scale(attackBox.Collider.size / 2, attackBox.Collider.transform.lossyScale), Quaternion.identity, wasGrabByPlayer ? whatPlayerHit : whatEnemyHit); // Foreach collider, check if it is an available target, and if so it this ! foreach (Collider _collider in _touchColliders) { if (_target = _collider.GetComponent <TDS_DamageableElement>()) { _hasHitEnemy = true; _damages = Random.Range(minDamage, maxDamage + 1) + bonusDamage; _hitElements.Add(_target.PhotonViewElementID, _damages); } else if (_collider.gameObject.layer == LayerMask.NameToLayer("Ground")) { _hasHitGround = true; } } if (_hasHitEnemy) { Hit(false); } else if (_hasHitGround) { Hit(true); } return(_hitElements); }
/// <summary> /// Detect all damageable elements and get these who are in its cone of sight /// Get the closest one and set its state to calculating path /// </summary> private void Search() { TDS_DamageableElement[] _elements = Physics.OverlapSphere(transform.position, enemySight.DetectionRange).Select(c => c.GetComponent <TDS_DamageableElement>()).Where(e => e != null).Where(e => e != this).ToArray(); if (_elements.Length == 0) { return; } bool[] _detected = new bool[_elements.Length]; for (int i = 0; i < _detected.Length; i++) { Vector3 _direction = _elements[i].transform.position - transform.position; Vector3 _ref = facingSide == FacingSide.Right ? Vector3.right : Vector3.left; if (Vector3.Angle(_direction, _ref) < enemySight.DetectionAngle) { _detected[i] = true; } else { _detected[i] = false; } } if (!_detected.Any(d => d == true)) { return; } List <TDS_DamageableElement> _targetedElements = new List <TDS_DamageableElement>(); for (int i = 0; i < _elements.Length; i++) { if (_detected[i]) { _targetedElements.Add(_elements[i]); } } currentTarget = _targetedElements.OrderBy(e => Vector3.Distance(e.transform.position, transform.position)).FirstOrDefault(); currentState = EnemyState.CalculatingPath; }