// Start is called before the first frame update void Start() { if (swarm == null) { swarm = Instantiate(swarmPrefab).GetComponent <SelectionSwarmFollow>(); } swarm.SetFollowTarget(targets[0]); }
//sets target to nearest enemy bool GetNearestEnemy() { float minDistance = 9999f; GetVisibleEnemies(); foreach (var enemy in _visibleEnemies) { float distance = Vector3.Distance(enemy.transform.position, transform.position); if (distance < minDistance) { _currentTarget = enemy.gameObject; _swarm = Instantiate(_targetingVFX, transform.position, Quaternion.identity); _swarm.SetFollowTarget(_currentTarget.transform); minDistance = distance; } } if (!(_currentTarget == _aimingReticle || _currentTarget == null)) { return(true); } return(false); }
private void Update() { //if player changes target and no target is selected, target nearest enemy if (_player.AimToggle && _currentTarget == _aimingReticle) { OnTargetLock?.Invoke(); GetNearestEnemy(); } else if (_player.AimToggle && _currentTarget != _aimingReticle) { OnTargetLock?.Invoke(); _currentTarget = _aimingReticle; _swarm.SetFollowTarget(null); } if (_currentTarget == null) { if (!GetNearestEnemy()) { _currentTarget = _aimingReticle; } } if (_currentTarget != _aimingReticle && _stickRelease) { if (_player.CycleTargetRight && Time.fixedTime - _inputCooldown > 0.1f) { GetNextEnemy(1); _inputCooldown = Time.fixedTime; _stickRelease = false; } else if (_player.CycleTargetLeft && Time.fixedTime - _inputCooldown > 0.1f) { GetNextEnemy(-1); _inputCooldown = Time.fixedTime; _stickRelease = false; } } if (!(_player.CycleTargetLeft || _player.CycleTargetRight)) { _stickRelease = true; } /* * if (_currentTarget != _aimingReticle) * { * _targetingReticle.gameObject.SetActive(true); * _targetingReticle.position = Camera.main.WorldToScreenPoint(_currentTarget.transform.position); * } * else * { * _targetingReticle.gameObject.SetActive(false); * } */ //if currently targeted enemy moves outside of range, target nearest enemy if (_currentTarget != _aimingReticle && Vector3.Distance(_currentTarget.transform.position, transform.position) > _maxRange) { GetNearestEnemy(); } //if no enemy is locked, aim with controller or mouse if (_currentTarget == _aimingReticle) { ManageAiming(); } //Orient player towards current target if (_player.CurrentState != PlayerBase.PlayerState.Dead) { LookAtTarget(); } }