private void spawnEnemies(int amount) { for (int i = 0; i < amount; ++i) { // Get a random sector (excluding the middle sector that the player spawns in) to spawn the enemy in int _randomSectorIndex = Random.Range(0, Level.childCount); if (_randomSectorIndex == Mathf.CeilToInt(GameManager.Instance.Level.childCount / 2.0f) - 1) { ++_randomSectorIndex; } Transform _sectorToSpawnIn = Level.GetChild(_randomSectorIndex); Transform _enemy = spawnShip(EnemyPrefab, _sectorToSpawnIn.position, _sectorToSpawnIn).transform; // Get a random position near the center of the sector and set that as the enemy's position _enemy.position = ProjectConstants.PickRandomPositionNearby(_sectorToSpawnIn.position, 0.0f, ProjectConstants.SECTOR_WIDTH / 3); } }
void Update() { // Move the enemy towards its current target position and pick a new target when reaching it transform.localPosition = Vector2.MoveTowards(transform.localPosition, m_targetPosition, MovementSpeed * Time.deltaTime); if (transform.localPosition == m_targetPosition) { m_targetPosition = ProjectConstants.PickRandomPositionNearby(transform.localPosition, m_minRoamingDistance, m_maxRoamingDistance); } // Get the sector the enemy is currently in and adjust the enemy's position and target according to it if needed m_currentSector = getCurrentSector(); // Shoot at the player when close enough if (GameManager.Instance.PlayerShip != null && Mathf.Abs(GameManager.Instance.PlayerShip.position.x - transform.position.x) <= ShootingRange) { shoot(transform.position, (GameManager.Instance.PlayerShip.position - transform.position).normalized, ProjectileSpeed, transform.parent); } }
public override void Initialize() { // Get the sector the enemy is currently in and get a random position for the enemy to roam to m_currentSector = getCurrentSector(); m_targetPosition = ProjectConstants.PickRandomPositionNearby(transform.localPosition, m_minRoamingDistance, m_maxRoamingDistance); }