perjantai 1. joulukuuta 2017

Unity platformer - enemy

NME17AP

Lukuvuonna 2017-2018

This time we're going to create a Mario game styled enemy. Player will take damage from the enemy if he bumps into it from the side. Player can also kill the enemy by jumping on top of it.

First you need to make changes to your health bar. If you haven't created global float variable for health. Do so by choosing global variables tab and write a name for your float variable. Also set it to 1 which is the players max health.



Create FSM for the health bar. This FSM will have two states and one event in it.


First state has the float changed action. This will check every frame if the health float is changed. If the global health variable is changed the the state will send the fill event.


Second state has the set property event. We need to change the property of the UI sprite, so drag and drop your health UI component to the target object section. Select fill amount as the property and your health variable to the set value section. Make this state send finished event and link the transtion back to the first state.

Next import your enemy sprite to the game and drag it to the scene. If you have idle, movement and death animations for the enemy then create them as well and connect those in the animator window similarly to the player animator.

Create two box colliders for the enemy. One box collider is normal sized while the other is small box on top of the enemy. Click the trigger option active for the small box.


Then it's time to choose FSM component. First FSM will do damage to the player. For this FSM you'll need three states, one event called damage and one global event called no damage. First state has a collision 2D event action.  Select the player as collide tag and make the action send damage event.


Second state has float subtract action. Select the player health as the float variable and 0.25 as the value for subtract section. Add wait action and wait for 1 second before sending finished event.


Third state has global no damage transition. This is done by clicking the state with the right mouse button and selecting global transition. This state will be activated when the player kills the enemy so that the player will not take damage while enemy goes through its death animation.

Next create new FSM component for the enemy. This will be the enemy death FSM. You'll need three states and one event called enemy death.


First state has trigger 2D event action. Choose player as collide tag and your enemy death event for the send event section.

Second state has 4 different actions. First action is set animator bool. This action is needed if the enemy has a death animation because the animation will be activated in this state. Second action is set property. For this you'll need to drag the collider that has no trigger to the target object section and select enabled to property. Third action is send event by name. In this state you'll send the no damage event to the enemy's player death FSM. Last action is wait in which you'll need to wait time it takes for the enemy to complete the death animation and after that make this action to send finished event.



Last state in the enemy death FSM has the destroy self action which will destroy the enemy and remove it from the game.



Ei kommentteja:

Lähetä kommentti