- Home /
Enemy Spawning
I am trying to get an enemy to spawn in 10 different locations using Vector3 or just anchoring the spawn to an invisible object. I need it so that when a collider, attached to the player, collides with the location the enemy will spawn. It will then chase the player for about a minute and will then disappear. There must only be one enemy at a time on the map. My only problem here is I'm not quite certain on how to code this. I've gone through the tutorials on the Unity site but none of them specify how to do this.
Answer by JusticeAShearing · Jul 05, 2014 at 04:03 PM
Well, this playlist isn't from the Unity Site as far as I know, but he has enemies that spawn, so he could be of use to you.
https://www.youtube.com/playlist?list=PLB4DA4F8BDC3B82EE
This question speaks about spawning scripts, but isn't explicit about what to apply it to. Perhaps you know. If so, please tell me!
http://answers.unity3d.com/questions/418587/why-does-my-tree-script-not-work.html
Hopefully this helps you as much as it helped me.
I'm trying to get an enemy AI to spawn. So it will be the actually character that is going to chase the player throughout the game. The YouTube series didn't quite touch on the spawning I'm looking for but coincidently it answered a few questions I was going to have to figure out later like the player health!
I have found more for you, out of the two hundred videos. Sorry if you checked them already and found them to be useless, but here they are:
This one deals with randomly spawning items, which should be convertible to enemies.
https://www.youtube.com/watch?v=EvF$$anonymous$$zj-_4A8&list=PLB4DA4F8BDC3B82EE∈dex=95
These two deal with AI.
https://www.youtube.com/watch?v=-fepHdO5jnE&list=PLB4DA4F8BDC3B82EE∈dex=127
https://www.youtube.com/watch?v=hOww54dF9Ac&list=PLB4DA4F8BDC3B82EE∈dex=129
Answer by Stobby · Jul 06, 2014 at 10:11 AM
Hi!
You could code this by defining each spawn location as an empty game object with a simple triggered collider (capsule collider or anything else which suits your needs). Every spawn game object will have attached a script which will use the event OnTriggerEnter() to do two things:
Look if an Enemy exists
If the space is empy, spawn an enemy in the location.
In order to check for enemies the enemy prefab will have to be tagged as "Enemy".
I attach a pseudo-code example
void OnTriggerEnter(Collider other)
{
//If somebody who's not the player is colliding, don't spawn
if(other.tag != "Player")
return;
GameController handle2otherEnemies= GameObject.FindWithTag("Enemy");
if(handle2otherEnemies!=null) //Ok, there is another enemy in game
return;
else //There is nobody
Instantiate(Enemy,transform.position,transform.rotation);
}
Just remember to:
create a public GameObject variable named Enemy linked to your Enemy prefab
Tag your Enemy prefab as "Enemy" (or whatever you like) and the Player as "Player"
Let me know if you need further assistance!
How should I create the Game Controller or should this script be the game controller?