keskiviikko 15. helmikuuta 2017

UNITY FPS - Enemy spawning and shooting

NAV16AP

3. jakso lukuvuonna 2016-2017

Create a weapon to your enemy just like you did for the player and place in it the hierarchy so that weapon has a holder, the weapon itself is child to the holder and spawning point for the enemy bullet is child to the weapon.

Create health fsm for the player in the same way you did for the enemy.


You'll need two events death and damage which is a global event.  Create 1 float variable called health.


In the first state set health to 10 with set float value action.


Next state has float compare action which compares the health variable to 0. Make death transition for this state and select it in the less than and equal sections. The death transition goes to third state.
Third state has only restart level action so that when player dies the level is restarted.


Last state has the damage global transition. This is needed for the bullet to do damage. Select float subtract action and choose health for your float variable and 1 as the number to subtract. Make finished transition to the second state that has the float compare action.


Also make a new bullet for the enemy. The bullet will be almost the same as for the player.


First state has two events: hit and destroy. Remember to make game object variable named player object and float variable named speed. Select the speed for that variable or the bullet won't move.



Choose three actions: translate, raycast and wait for the first state.

Translate has the speed variable in the z axis.


In raycast write 1 in the z axis for direction, 0.9 for distance and select hit as hit event and store the hit object as player object. 

Wait for two seconds before sending event finished to the destroy state.


Second state has compare game object tag action in which you will compare your game object variable to the player tag. True event is hit player and false event is not player. You'll need to make these events in the event tab. Link true event to third state and false event to destroy state.


Third state has the send event by name action. Send the event to the player object and choose right fsm and event for the action. 

Create finished transition that links to the fourth state called destroy which has the destroy self action.


Next create a new fsm called enemy shoot for the enemy.


Create a bool variable named sight and two events: fire & keep firing.



In the first state you're going to have two actions. First is get fsm bool in which you select from your enemy movement fsm the insight variable and store it as the newly created sight variable. Bool test has the sight as variable and it sends is true event fire.


Second state has 4 actions: get fsm bool, bool test, create object and wait.
Get fsm bool has same selections as in the first state. Bool test has sight as bool variable and finished in false event. Create object has your enemy bullet prefab as the created object and enemy bullet spawn as spawn point. Wait action send finished event after 0.06.



Third state has the same actions except for the create object. In wait action choose the time to be 0.9.


Lastly you're going to spawn more enemies. Make an empty game object and name it as game controller. Select new fsm for it.


Create four variables. First is named position and it's a vector3 variable. Then you need two float variables named zaxis and xaxis. Create a global int variable called enemy.  Also create new event called create enemy.


In the first state choose action called random float and use it two times. The min and max values for these depend on your game scene space. You can create an empty object and move it around to check in what area you want your enemies to spawn in. After your have selected the values and stored those in the float variables make finished transition to the second state.


Second state has three actions: set vector xyz, create object and int add.

In the set vector xyz choose your xaxis and zaxis float variables in corresponding section. For the y value choose the y value from your enemy so it wills spawn at right height. Select your position variable in vector 3. Create your enemy prefab in the create object and select position variable as position. Int add actions has global enemy int variable  and add 1 each time enemy has spawned.


Third state has int compare action. This will compare your global enemy variable to a number you choose. This number will represent the number of enemies in the field. If the enemy variable is less than the number send event create enemy to the first state in less than section.



Lastly you want your enemy count to decrease. Make int add action in the enemy health fsm before the destroy self action. Choose your enemy variable in the variable section and make section add -1.


Ei kommentteja:

Lähetä kommentti