- Home /
Shoot 'Em Up: Best way to instantiate enemies
First off, I'm not an english native speaker, so sorry in advance if it's a bit hard to read.
I am developing a 2D shoot'em'up and I've been working on boss fights, enemy scripts, designing backgrounds and other stuff.
If you've played any 2D game you may know that enemies don't appear at random, they do when the camera/player reaches a certain point in the map.
My idea was to create a GameObject and then put inside both camera and player and make it move towards different waypoints and as soon you reach one, an enemy appears with Instantiate and AddComponent.
The question is simple, if you are developing a 2d shoot'em'up/space shooter and you want to instantiate enemies at certain points, what would you do? Please, don't tell me "there are different ways depending on what you want", I want to know what you would do.
Here's what I have in mind, I haven't tried it out yet, just to show you my idea. Is that what you would do or something completely different?
void Update(){
if (move == true){
cameraAndPlayer.transform.position = Vector3.MoveTowards (cameraAndPlayer.transform.position , waypoints[currentWaypoint].position, speed);
}
if (cameraAndPlayer.transform.position == waypoints[currentWaypoint].transform.position){
if (currentWaypoint == 1){
enemy = Instantiate (Resources.Load ("Prefabs/enemies/enemy1")) as GameObject;
enemy.AddComponent<enemy1>();
}
else if (currentWaypoint == 2){
enemy = Instantiate (Resources.Load ("Prefabs/enemies/enemy2")) as GameObject;
enemy.AddComponent<enemy2>();
}
currentWaypoint++;
}
}
Answer by Eno-Khaon · Dec 07, 2015 at 02:54 AM
If I were to approach this from the basis of pre-fabricated levels (or, more specifically, without random enemies unless specifically triggered during the level), I would probably take either of two approaches:
1) Instantiate enemies: I would create spawn groups either as prefabs or as data structures (and instantiate each enemy contained inside), then give each enemy a flight path pattern and shooting pattern of their own. Although there's the slight possibility it might have meaningful performance overhead with enough enemies generated at once, it's likely no worse than creating the tremendous number of projectiles they'll fire.
or...
2) Pre-place the enemies in a level: Create groups ahead of time and place them in the level, define all the characteristics of each enemy starting where they'll first become active, then deactivate their script. When the camera reaches a specific position (or game time reaches a specific time, depending on preference or emphasis on musical cues), activate the scripts of all enemies contained in the group, then call an "Activate()" function in the enemies to get them moving (or, alternately, to stagger them based on yield instructions). The key benefit to this option would be that you can define any script variables per ship in the editor without needing additional script control or prefabs per enemy.
I know you don't want to hear that it "depends on what you want", but I think this really depends more on what is necessary to get the result you need. If you're creating an endless game, you'll definitely need to Instantiate the enemies. If your game is meant to play out linearly, then it depends on how many enemies you're creating at a time, how many you're creating in total, and whether any one approach is giving you more trouble than another.
Thank you, I'll try out both ways using my waypoint system and then decide which is the best option. I'm pretty sure the waypoint/instantiation system is a common thing but pre-placing the enemies could be another good solution.
It would be cool if somebody who developed a "not random" space shooter could explain his method, maybe he used something completely different.
Answer by hubi037 · Dec 07, 2015 at 04:51 PM
I would do this quite different. Each Waypoint would be a gameobject with a large sphere collider set to Trigger, denoting the area that the player needs to enter for an enemy to spawn.
Then you can have a script on the Waypoint that does this
void OnTriggerEnter(Collider other)
{
//instantiate your enemy here
}
Of course you should set up your collision layers in a way so that only the player can collide with your waypoint trigger.
Edit: You should probably disable the trigger after spawning your enemy so that this can only happen once.
The only risk I see with that idea would be purely based on design intent. If it's based on the player reaching a specific region, then that means the player could rush forward and a dense wall of enemies spawns all at once, then they pull back again and no new enemies arrive for the maximum possible time.
It would certainly provide a different kind of experience, but a typical design for a shoot 'em up will send the enemies at a more-fixed rate, which the player would be unable to personally influence.
It could work if the camera+player gameobject.localposition is the trigger object, not the player, the camera will always be moving forwand (or diagonally), never backwards, as soon as it touches the trigger collider, it gets disabled.
Tne only problem is creating a single script for each collider ins$$anonymous$$d of just one level script that handles every waypoint or whatever used to instantiate the enemies.
Your answer
![](https://koobas.hobune.stream/wayback/20220612051609im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate Explosion Position 2 Answers
Simple Instantiate gameObject problem 3 Answers
How to instantly move a newly instantiated prefab? 2 Answers