- Home /
What is the best way to spawn enemies?
My Game: 2d Side scroller like OMG pirates! game.
Problem: I need to know what is the best way to spawn enemies in code.
My solution: My solution is this for now, I will have transparent trigger objects on the path of the player. When the player passes over it, a script function does an instantiate on several spawnpoint game objects(invisible game objects on the map from which the enemy tanks get Instantiated). my enemy tanks are prefabs.
Problem in my solution: I will write an instantiate() line of code. This will generate one tank, I want to do 2 or 3 at a time. Does this mean I have to run a for loop and have multiple different named spawnpoints on the map?
$$anonymous$$aybe. I think yes. But its hard to tell without seeing a little of the actual code.
Answer by kgoswami · Sep 13, 2011 at 08:43 PM
Solution: I have used this . When Player character touches a trigger named "TriggerSpawn" spawnenemy object is spawned at a short distance from the player. I will later calubrate this distance so that the enemy is spawned offscreen from the iphone screen. It seems to b working for generating one enemy. I want to generate more, but ow the solution is simple, I just have to duplicate the trigger object. It has no script running, its just a long invisible line perpendicular to my character's line of path with an isTrigger checkbox ticked. self: Transform is a reference to the player object itself.
var spawnenemy:Transform;
var self:Transform;
function OnTriggerEnter(collider: Collider)
{
if(collider.gameObject.name=="TriggerSpawn")
{
var enemy=Instantiate( spawnenemy, transform.position + ( transform.right * 10), Quaternion.identity );
//var enemy2=Instantiate( spawnenemy, transform.position + ( transform.left * 10), Quaternion.identity );
enemy.GetComponent("TurretControlEnemyTank").LookAtTarget=self;
//enemy2.GetComponent("TurretControlEnemyTank").LookAtTarget=self;
}
//spawnenemy.LookAtTarget=gameObject;
//Debug.Log("SPAWN ENEMIES!");
}
Answer by syclamoth · Sep 13, 2011 at 10:09 AM
If you wanted an easier way to implement a 'spawn points' system, you could use an array or list of Transform components, and then in the editor drag and drop each spawn point in to the list. In your script you would have the 'pos' Vector3 be determined something like
public Transform[] spawnPoints;
Vector3 position = spawnPoints[Random.Range(0, spawnPoints.Length)].position;
You could change the spawn direction the same way.
As for the player triggering the spawn, you could put your spawn script on the trigger volume, and then have it one-shot spawn a (possibly random) number of enemies at random spawnpoints from your list like this-
for(int i; i < Random.Range(3, 10); i++)
{
Transform currentSpawn = spawnPoints[Random.Range(0, spawnPoints.Length)];
Vector3 position = currentSpawn.position;
Quaternion rotation = currentSpawn.rotation;
Instantiate(prefab, position, rotation);
}
This would spawn between 3 and 10 enemies at random points determined in the editor.
Of course, if you just wanted a random location in a circle, there's always Random.insideUnitCircle- you could state a centre point and spawn enemies around it like that.
Answer by timsk · Sep 13, 2011 at 09:41 AM
// Instantiates a prefab in a grid
var prefab : GameObject;
var gridX = 5;
var gridY = 5;
var spacing = 2.0;
function Start () {
for (var y = 0; y < gridY; y++) {
for (var x=0;x<gridX;x++) {
var pos = Vector3 (x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
taken from unities instantiating prefabs discussion page Here
That is just one way to do it, another way would be to create empty game objects, called "Spawn Points" and attach the instantiate script to each one. This is much messier tho.
Your answer
Follow this Question
Related Questions
GameObject is spawned twice 0 Answers
Make enemy damage the player,Enemy do damage to player Script 1 Answer
Enemy reorienting for player 1 Answer
How to spawn a prefab on a trigger? 3 Answers
Phantom Instantiation at Origin 0 Answers