maanantai 15. elokuuta 2016

Unity platformer - character movement 2

NAV15AP

1. Jakso lukuvuonna 2016-17

Last time you learned how to make basic movement for a character in Unity. Now we're going to learn a different and perhaps even easier way to do this.

First we need to create two events: flip left and flip right. Then we need two new states: left and right. At the start state we need to connect the flip left transition event to left state and do the same for right. Then we need 4 actions. 2 actions are get key down and you need to assign movement buttons to them. A button for moving left and D button for moving right. These are needed for scaling the sprite in the two other states.



We also need action get axis. This is for defining the movement. Axis name will be horizontal because we need the horizontal axis to move left and right. The axis name can be found in edit -> project settings -> input. It has automatically wasd and arrow key movement in it. Then we need to put multiplier in which is the max speed character is moving. We also need to store the value and for that we need to create a new float variable named speed.




Lastly we are going to put set scale action in left and right state. This will scale the character like we did in the last movement assignment. Be sure to use 1 for moving right and -1 for moving left. If you have decided to scale your character so it's bigger or smaller in the game, then you need to match that scale in the set scale action.

Next we are going to do the ground detection differently. Last time we used raycast and now we're going to use collision.

First we need to make a new FSM. Name the FSM as you see fit. For this we need to create two events: in_air and collision. These are going to be used as global transitions. You can put a global transition in FSM by right clicking it and choosing: add global transition. We also need another state named as grounded.


In the first state we are going to use action Collision 2D event. This is for the detecting if character is colliding with an object that has a specific tag. We need to put tag for our ground object and use the same tag in this action. Then we need to send the global transition named collision. In the first state we also need to use action set property. In this actions we need to define that the FSM_JUMP is not enabled. In the grounded state we need to set property for FSM_JUMP and check enabled section.

Lastly we need to send event from FSM_JUMP to FSM_GROUND_CONTROL. This is done with send event by name action. There we can define which global event we are going to send to which FSM. We choose FSM_GROUND_CONTROL from the list and write the event name in_air because we want the ground control to know that we are jumping and not touching the ground. Then the ground control can disable the jumping FSM and character won't be able to jump again while in mid air.


Ei kommentteja:

Lähetä kommentti