private void CreateEnemy(EnemySpawnType enemySpawnType) { Vector3 enemySpawnPosition = LeftWaveGenerator.transform.position; Enemy.EnemySpawnPoint spawnPoint = Enemy.EnemySpawnPoint.Left; if (enemySpawnType == EnemySpawnType.Right) { spawnPoint = Enemy.EnemySpawnPoint.Right; enemySpawnPosition = RightWaveGenerator.transform.position; } GameObject newEnemy = GameObject.Instantiate(( GameObject )(Resources.Load("Prefabs/GameEnemy", typeof(GameObject)))); if (newEnemy) { newEnemy.name = "Enemy" + currentEnemyIndex; enemiesList.Add(newEnemy); newEnemy.transform.position = enemySpawnPosition; Enemy enemyScript = newEnemy.GetComponent <Enemy>(); if (enemyScript) { enemyScript.SpawnPoint = spawnPoint; enemyScript.SingleEnemyDied += OnEnemyDied; } } }
void FixedUpdate() { currentSpawnTimer += Time.fixedDeltaTime; if (currentSpawnTimer >= BaseSpawnTime) { currentSpawnTimer = 0; float randomNumber = Random.value; EnemySpawnType spawnType = EnemySpawnType.Left; if (randomNumber >= 0.5f) { spawnType = EnemySpawnType.Right; } CreateEnemy(spawnType); currentEnemyIndex++; if (currentEnemyIndex % ScalePeriod == 0) { IncreaseDifficult(); } } }
public void Spawn(string pawn, EnemySpawnType type) { switch (type) { case EnemySpawnType.AROUND_RANDOM_PLAYER: // Find a player to spawn at, must not be dead. Player player; do { player = Player.AllPlayers[Random.Range(0, Player.AllPlayers.Count)]; }while (player == null || player.Health.IsDead); // Set min and max spawn distances float minDst = 20f; float maxDst = 30f; // Get the player's location. Vector2 playerPos = ((Vector2)player.transform.position.ToInt()).ClampToWorldIndex(World.Instance); // Make an offset until it is a valid position... Vector2 offset; Vector2 position; int loop = 0; do { offset = Random.insideUnitCircle.normalized; offset *= Random.Range(minDst, maxDst); offset = offset.ToInt(); position = (playerPos + offset).ClampToWorldIndex(World.Instance); // Avoid infinite loop when the enemy simply can't be spawned... loop++; if (loop == 100) { Debug.LogError("Enemy {0} can't be spawned after trying to find a spot {1} times! Centered around player {2}. Spawning right on top of player :D".Form(pawn, loop, player.Name)); position = playerPos + new Vector2(0.5f, 0.5f); break; } }while (!World.Instance.TileMap.GetLayer("Foreground").IsSpotWalkable((int)position.x, (int)position.y)); Pawn.SpawnPawn(pawn, position + new Vector2(0.5f, 0.5f)); break; } }
// This is only called for enemies. public IEnumerator SpawnIn(EnemySpawnType spawnType) { // Make sure the enemies are spawned with the rotation of the spawn point // so make it face the direction facing the trigger area for the current // battle zone. aiState = AIStates.Spawn; Vector3 moveDir = myTransform.forward; // Entered area becomes true after exiting our spawn spot trigger bounds // for the current area. while(!_enteredArea) { if(spawnType == EnemySpawnType.Normal) _charMotor.Move(moveDir, false, false); else if (spawnType == EnemySpawnType.Find_Way && !targetForNavMesh) targetForNavMesh = FindNearestTarget(); yield return new WaitForSeconds(0.0001f); } // We can now target a character. // I just change the character's name to the name given in the // corresponding enum. Just a personal preference. gameObject.name = _charStatus.myCharacterEnemy.ToString (); aiState = AIStates.Wander; _charStatus.Vulnerable = true; // We can now be hit. // We are now added to the enemyTargets list so players can target us. Manager_Targeting.instance.enemyTargets.Add (myTransform); StopCoroutine ("SpawnIn"); yield break; }