maanantai 3. lokakuuta 2016

Unity clicker - spawning enemies

NAV16AP

1. jakso lukuvuonna 2016-2017

Now you’re going to spawn new enemy after the player has killed the previous one. Create new FSM for the enemy health. For this you’ll need many states. Start with mouse down transition event in the start state.


In the next state use action int operator. With this you’re going to subtract desired amount from the enemy health. Create new variable for the health and use int as type. Choose a number that you want the enemy health to be. Now use your created health variable as the integer 1 and for integer 2 a number you want to subtract from the health. Use subtract as the operation and store the value as your health variable.


The third state will compare the enemy health to 0 and determine which event is activated based on that comparison. Select the int compare action and again your enemy health as the integer 1. For integer 2 use 0 which will mark the death of the enemy. Create two events: dead and not dead. Use dead in equal and less than section and not dead in greater than section. Now pull not dead transition event to the start state and dead transition event to a new state.


In the next state you’ll be creating random enemy. For this you need different looking enemies. Create new int variable which you need when choosing a new enemy to spawn. In the new state use two actions: random int and int switch. For the random int actions select 1 as min and the max depends on how many different enemies you’re going to spawn. For the int switch choose your enemy int variable as the in variable. The number of switches is the number of different enemies you have. Then create new events to spawn the enemies. You can name the events as you like but remember to connect the right int to the right event.


Lastly create as many states as you have enemies. In my example I have three states because I use three enemies. There are two actions that is needed for the spawning of enemy. First is the create object action. For the game object in that action you’ll need prefab. Create prefab by right clicking in the project folder. Select the prefab option and name your prefab. Next drag your game object from the scene to the newly created prefab. Now you can use the prefab to spawn enemies that have same exact values. When you edit prefab the values will change for every object that is brought to the scene from that prefab. More about prefabs can be found in the unity manual: https://docs.unity3d.com/Manual/Prefabs.html



Now you can finish the prefab by editing it. Choose the prefab as the created game object. Last action will be destroy self which will delete the enemy prefab after spawning a new one. You need to create as many separate prefabs as you have different enemies in your game.

Remember to make changes in your timer FSM. In the lose state change for the send event by name action the game object and create more of the same action for different enemy prefabs.

Ei kommentteja:

Lähetä kommentti