keskiviikko 13. joulukuuta 2017

Unity platformer - hanging

NME17AP

Lukuvuosi 2017-2018

We're going to make hanging mechanics for the character. For this assignment you'll need to have two more animations to your character: hanging idle and movement animations. Also create a platform that has vines below it.

Create your hanging animations and link them in them in the animator window. Create two new bool parameters for idle and moving hanging.



Next add new FSM component for the player. This FSM has four states, two global events: hang & don't hang and three normal events: left, right & drop.


The first state has don't hang as a global transition. It also has four actions. First action is set property in which you'll need to drag and drop the player movement FSM component to target object section. Remember to tick the enabled box. This will make the movement FSM active when player is not hanging.

Second and third actions are both set animator bool. Select your hanging and idle hanging parameters to it and don't click the value box because we want those animations to be inactive in this state.

Last action in this state is set property. Drag the rigidbody 2D component to the target object section and select gravity scale as property and 1 for the value. This makes gravity active when the player is not hanging.


Next state has the global hang event as well as left, right and drop event.

You'll need same four actions as you had in the first state, so copy and paste those from that state to this one. This time the set property for the player movement FSM has no tick in the set value box and gravity scale is set to 0. Also tick the value box for the set animator bool action for idle hanging.

Add three get key down actions. Select key D for the first one and make it send right event. Next select key A and make it send left event. Last get key down action has S as the key and it sends drop event.


Third state is for hanging and moving. In this state you'll need five actions and two events: drop & finished. First action is set animator bool which has the parameter hanging activated. Second event is get key down. In this action choose S as the key and make it send drop event which is linked back to the first state. Third action is get key up. Choose A as the key which sends the finished event back
to the first state. Fourth action is set scale. This is needed when player changes direction. If the player faces right by default then you'll need -1 in the x axis. Last action is translate which is used to move player. Put a number in the x axis section. If player moves left then you'll need to add minus sign before the number.


Last state is similar to the third state. Just change D to the get key up action and don't put negative numbers to set scale and translate actions.

Then create new FSM for the hanging platform. This FSM has three states and two events: activate hanging & deactivate hanging.


First state has trigger 2D event action. Choose player as collide tag and make it send activate hanging event.

Second state has also trigger 2D event action. This time choose on trigger exit 2D for the trigger section, player as collide tag and make it send event deactivate hanging. Second action for this state is send event by name. This send information for the player hanging FSM so that it knows to activate hanging mode. Choose game object FSM for the event target, remember to select specify game object and drag the player object to it. Choose your hanging FSM for the FSM section and select the global hang event to the send event section.


Last state has also the send event by name action. Now write don't hang in the send event section and otherwise keep it same as before.



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.