// Gets clsoest target with tag "PlayerTile" public void GetTarget() { var allTargets = GameObject.FindGameObjectsWithTag(Constants.TAG_PLAYER_TILE); if (allTargets.Length > 0) { var currentMinDistance = Mathf.Infinity; foreach (var go in allTargets) { var tempD = Vector2.Distance(gameObject.transform.position, go.transform.position); if (tempD < currentMinDistance) { currentMinDistance = tempD; _currentTargetGo = go; } } } if (_currentTargetGo != null) { _state = Enums.SpawnState.MoveToTarget; } }
// Update is called once per frame void Update() { // If the spawn has awoken if (Time.time >= _wakeUpTime && !_wokenUp) { _state = Enums.SpawnState.LookForTarget; _wokenUp = true; } // If spawner data has been set if (_data != null) { if (_state == Enums.SpawnState.LookForTarget) { GetTarget(); } else if (_state == Enums.SpawnState.MoveToTarget) { // If current target has not been destroyed already. if (_currentTargetGo.activeInHierarchy == false) { _state = Enums.SpawnState.LookForTarget; } // If spawn is not next to target if (Vector2.Distance(transform.position, _currentTargetGo.transform.position) >= _data._range) { MoveToTarget(); } else { _state = Enums.SpawnState.DamageTarget; } } else if (_state == Enums.SpawnState.DamageTarget) { // If current target has not been destroyed already. if (_currentTargetGo.activeInHierarchy == false) { _state = Enums.SpawnState.LookForTarget; } DamageTarget(_currentTargetGo); } } if (_hp <= 0) { gameObject.SetActive(false); } }