Quantcast
Viewing all articles
Browse latest Browse all 1488

Need help with enemy respawn script

Hello, this is the script. Basically, it's mixed with the enemy health script so it's easier for me, I want to spawn more enemies as soon as the group that spawned earlier dies, for example, the game starts with only one enemy, as soon as he's dead, I want two more enemies to spawn and when they are both dead I want to spawn three and so on. The problem with this script is that I want them to spawn on random positions but they always spawn on the same position and one at the time. Looking at the code the group of enemy would spawn when just one is killed and that's wrong, so, in short: 1) How can I spawn a group of enemies when the group that spawned earlier is dead? 2) How can I spawn the enemies in random positions? This is the code I made public var damage = 15; private var health = 100; private var enemyNumber = 2; function Start () { } function Update () { if(health < 1) { animation.Play("death"); Destroy(gameObject, 0.31); } Debug.Log(health); if(health < 0) { health = 0; respawn(); } } function OnCollisionEnter (bulletCollision : Collision) { if(bulletCollision.gameObject.tag == "bullet") { health -= damage; Debug.Log("Hit enemy"); } } function respawn() { var Enemy = GameObject.FindGameObjectWithTag("Enemy"); var enemySpawns = GameObject.FindGameObjectsWithTag("spawns"); for(var i = 0; i < enemyNumber; i++) { var spawn = Random.Range(0, enemySpawns.Length); Instantiate(Enemy, enemySpawns[spawn].transform.position, transform.rotation); } enemyNumber++; } EDIT: Just found out that more than one enemy spawn when one is killed, the problem is that by spawning on the same place they (I'm using cubes at the moment), let's say sum up and it seems like there is only one cube. As I said above, I want them to spawn when the group that spawned earlier is gone and I want them to spawn to random places. EDIT2: The code remains the same but now the random spawns are working and it spawns more than one enemy at the time. Still, it spawns too many enemies and not when the group is dead.

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>