torstai 30. maaliskuuta 2017

UNITY - 3 ways to spawn enemies

There are many different ways to spawn enemies. Here are a few of them.

TRIGGER SPAWN

This is done by having an empty game object in the area. When player triggers the object it will create an enemy (or multiple) in specified or random places.

Create an empty game object and choose box collider for it. Make sure that is trigger has checkmark so that player can pass it.


Now create FSM for the empty object and three states in the FSM. 


First state has trigger event or trigger 2D event depending what kind of game you’re working on. Select your player tag that you have connected to the player to collide tag section and make it send spawn event.

Second state is used for randomizing enemy position but if you don't want to randomize then you need only two states in this FSM. This state has random float action. Choose min and max values for the float and store it as float variable. If you want to randomize all the axes then create three actions that are stored as their own float variables. Second action for this state is set vector3 XYZ. Use your float values set with random float action and store it as vector3.


In the next state you can spawn enemies with create object action. Choose your enemy prefab as the game object to be spawned and select position for it. If you randomize then choose your enemypos vector 3 value in the position.



TIMED SPAWN

Timed spawn is literally spawning enemies after some amount of time has passed.

Create empty object and FSM for it. In the first state, add wait action. You can also use random wait and choose timespan for it.

Next state will have create object in which you'll need to select your enemy prefab. Then link the second state back to the start state with finished event.

WAVE SPAWN

This is done by spawning multiple enemies at the same time then waiting their deaths before spawning a new wave.

Create empty object and FSM for it. Create a state that has int compare action and add global int variable called enemy. 



In the int compare action, choose your enemy as the variable and compare it to zero. Make it send spawn action if the enemy count is equal to zero (or less). Remember to put checkmark in every frame section so that it does the comparison all the time. 

Second state has int add and create object actions. In the int add choose enemy as the int variable and make the count equal to how many enemies you’re going to create. Create object will create your enemy prefab in the position you want to. You can also randomize position for the enemies.



For the enemy you need to have FSM for its death. In the last state before destroying the object select ind add action and -1 for the enemy int variable.


Ei kommentteja:

Lähetä kommentti