Create Flappy Game Mechanics with Unity & PlayMaker | Stefan Boeykens | Skillshare

Create Flappy Game Mechanics with Unity & PlayMaker

Stefan Boeykens, Architect-Engineer-Composer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
9 Lessons (43m)
    • 1. Introduction

      1:53
    • 2. Setting up playmaker

      4:50
    • 3. Flapping capsule

      5:39
    • 4. Checking collisions with the ground plane

      3:23
    • 5. Setting up the moving poles we need to avoid

      7:44
    • 6. Spawning poles from the game manager

      4:24
    • 7. Keeping the score

      9:56
    • 8. Finish the game

      4:24
    • 9. Conclusion

      1:14

About This Class

Remark: This course has been updated for Unity 2019.3

This course is a gentle introduction to small interactive games like Flappy Birds or other classics. Instead of making a clone, we will only go through the game mechanics: flapping or jumping, gravity and collisions, using Unity Physics. 

0a6c4ac8

The graphics will be deliberately crude: a white capsule and a few boxes. There are no textures, sprites, sounds or particle effects. We encourage you to make something of your own. 

You'll need to know the basics of Unity: creating GameObjects, navigating the 3D Window and adjusting components. We'll use the popular Playmaker extension from the Asset store, which is a visual coding system, providing a Finite State Machine implementation, requiring no custom scripting. We go through every step from scratch, although the final result can be downloaded as a package for you to study.

Transcripts

1. Introduction: welcome to discourse on using unity and playmaker. We want to recreate small game that resembles flappy birds. It's very basic. My name is safe, up weakens. I'm an architect, a tree from Belgium, and I just want to introduce you to some basic concepts. Game developed game mechanics setting up simply directions. And we will make use off the playmaker package for unity. This course will not focus on game graphics, sprites, images or effects, just the basic game mechanics that you can apply in many different situations. Let's give you an example before the rent result will look like press play like I kept so you can use Milo's or any dope, flap it up or down. See polls that are moving if we can avoid them or score increases continuously come to in our direction. And whenever we hit one of the polls, game is over. To get a score, have you get a restart button and the play. The game begins all over again, so let's get started. The only thing you actually need unity, obviously, and a playmaker. Adul was the playmaker system that allows you to at directions, reactions, behaviors and gave objects using ah, official way of coding, so it does not require you to write coats in C sharp, but it allows you to add behavior to objects in a fairly easy way. That's what we use in discourse. 2. Setting up playmaker: The first thing we do is setting up a new project. We choose the regular three D environment. Give it a name clapping. Security created project for us. The only thing we need to do right now is adding the playmaker unity package maximized. Yes, it startup. Look for my favorite assets, an import package in my project. Opportunity. Do its work and then playmaker launches first install. Do I look president star playmaker? Yes, I made a back up and it's not problem. It's a new project. So go ahead and the unity insults all the playmaker stuff inside his project that will take a while. Number concludes that back it again. Don't need. Yes, it's sort we know anymore. Then I set up the main playmaker interface. I open a playmaker editor and I prefer talk. That bottom site said my project with rights. So I have the hierarchy of the project seeing game viewing window playmaker panel and then the projects on the console. If there any errors. In addition, once we are inside playmaker, I also often used the action browser, so that's also window I often use, and I put it to the site next study inspector. Now the main interface is ready to start. You can set it up any way you want tear. It is important that square can see or wheat. First thing we do is creating a new three D object capsule. Create treaty object capsule inspected. We can check that it's placed in the center of the world. So far, we are not going to really tweak it any further, so we'll leave it at that. Then we'll use a quart objects, which we positioned above our drew, which will act as a ceiling stir the object sites, put it upon five units, top minus 90 degrees. So that is something that might prevent our player from jumping too high. I call this the upper plane. Make duplicates that will be our lower bling. I move it to the bottom, and we have to turn it in the other direction. Let's put it at minus five, and it's it 90 degrees. So now you've airplane lower plane capsule to finish things up, we'll set up our camera because we go for mostly to the look who said the projections off camera to photographic. We don't need a sky book so we just simply use it made solid color Andi to even out the lights. We'll just increase the intensity, so get a bit more flat. Look, it's not that important, Um, and also adjusts the clipping camera that's positioning that zero Ah, knots make its extent too much into the scene because we only needs a small space. So that's it. Now we're ready to start our development. If you press play, obviously nothing will happen. It's just a capsule between an upper and a lower plane. We know already to start petting actions. 3. Flapping capsule: right now, we'll set up something direction on our hero. Let's rename our capsule to here. That's a better name. Now we can at physics that components physics Richard Body to the objects. If you press play now you see that the object is just falling down and being held by the lower place. But we need to let some direction stop playing. Select a hero right click toe at a Phoenix State machine to the objects here we'll have start state. That's what happens when we first learned to came. Let's rename it Toe Idol. This is waiting for inputs, so we'll have to at an event an event that will happens. Transition between the different states off the object. First event will at do that that event step. Call it up, and whenever we called up event, it will launch a transition to the New states. New status. What will happen when we press up? Let's add a new states right click at ST and call it slapping, then at a transition, using up events to transition from the Idol state to the flapping states. But right now, both states are just empty. Nothing will happen. We need to add selections. We find the actions in the playmaker action step election Being you Playmaker presents all the possible actions that are installed by default. One would eat Newt right now can be found in Input Step Input Group and his first action. Any key sense an event when user hits any key or mouse button. Let's dragged out onto the state and we send an event event, which will be already prepared. The up event. The up events transitions from Idol state to the flapping states. Never press any key. We start flapping to be able to flap. Will use physics. So you go to the physics step physics Rupert and acidic the 2nd 1 toe at a force to the game objects director at Force Action onto the state flapping state. We need to point force into a certain direction. For that would use the Y direction not using a variable, but using a magnitude. He said it at 45. We don't want to set it in four smote force but enforcement pimples. It's one time pulls force that is pushed, pushing the capsule upwards. To finish, the states have to return a transition finished. I returned back to the Idol states. Something that is always important when you have user input is at the very small waiting action waiting actions help us. It's it's on the time group weight. Let's give a small delay off. 0.0. Want seconds and we link it to the finished event so automatically when the way it is finished event is called and return back to the Idol state. Now we contest Prestley Press. The mouse button on our here is launched upwards. Do it a few times, but eventually we'll see. Our hero is where What is he? Transform is pointing somewhere very far away, so it's falling somewhere into eternity. That was not the intention. Until we stop Lee, the hero is going way too fast and we have to control. We can control this in the region. Body component. First thing you need to is increased. The mosque, that's what it at eight. Let's try again, but it's better now. It's not looking too fast to steal at the problem, the event eventually going in the wrong direction. Second thing we can do is adding constraints to the rigid body strange and the only thing We want to allow its moving upwards in the Y direction. So we put constraints on xz and we don't allow any rotations. No, we can return, just play. And this much better we can push him. Gradually, a person flapping action, you receive a finished state machine switching between idol out flapping and the two planes ensuring that objects will not go off screen. That is a very basic mechanics off the flappy object. 4. Checking collisions with the ground plane: we will focus now on our lower plate. We at Phoenix State Machine to the objects. If the main ST and I'd only so we are waiting for something to happen and will define a new event event, it's called it offence hit. Whenever the object is hit, we will send the event at transition it and we'll do this with a collision action. Collective physics and use collision event. Text collisions between game objects at a frigid body or collider components. Whenever we collide with player, we want this collision to ensure that an event is sent. The hate of antisense. To properly find the player tech, we have to check that our hero is actually tacked. Go to the inspector and ensure that it sets to player. Now the collision. We don't react when the player is it who at New States I would call it hit by a player. So whenever we collides with the player, we transition to the hit by player states. What we'll do right now as a reaction to be hit by a player is send around an event all other state machines so they can react properly for that pattern. New events call the event clear. Waas hit And who made that event global? Checking this books play make It gives us a warning. Playmaker Global's created a small asset file that we can use if you want to ever copy the global events between projects. Okay, I still have to at the action. Good toe action step. I'm go to What? State? Missing machine and select sent Yvette Sense event between state machines. Some dogs and they went to ourselves. But we broadcast the events toe all other state. She's so they can react properly. We can exclude ourselves. We don't have to react our own and event. Who sent his one? We just created The player was hit global event That is the basic set up off the lower place and to see what will happen. You just press play whether he'll fall down and we'll see. Based on the collision event he transferred to the hit by player states and the player was hit event send around 5. Setting up the moving poles we need to avoid: in this lecture, we will set up the polls between which the player has to pass. The I D is to Adam as Treaty Cube object, which starts at the right and gradually moose towards the player. So it's not the player that it's moving balls conclusive, and whenever they come off screen, they will be removed again. That is a basic concept, so let's start from Basic Cube. Um, position them center, move them upwards and give them a vertical scale off about six. Of course, five. That will be the upper bowl. Let's give it to name. Oh Upper de Purple has exactly the same behavior as the lower plane. What we can do. It's right. Click on the finished state machine of the lower plane and copy it, go to the upper bowl and based it again. That way, it also reacts to hitting the player and moving to a state. What is his by the player and the event is broadcast to all other objects. Let's duplicate his poll. Make it the lower poll instead of five. He added to minus five. So now we have both bulls on a gap in between where the player campus. We group them together. So creating new, empty objects that's cool. It polls and at the upper and lower bowl as Children underneath the balls objects. Now they are grouped. It can be moved together. That is what will happen on this. Groups object. We also lead finished state machine implements the behavior. So at NFS, um, have any fine proper start state? Initially, we will put the poles to the right of the screen well beyond the edge of the screen. So we don't see them. That will be about a position off 10 or 15 something like that. And before we start placing them, we also give them a vertical offset. Sometimes they're a bit lower, sometimes a bit tired. For that, we want to embed a random positioning, so we get some variation between the poles. The first thing we do, it's calculate a random vertical offsets for debts will use one of the random actions and will use a teacher because then we can work in fixed steps random in teacher between minus tree plus three and after max Value me included. Several started into a new variable. Have a call it variable offset with his offsets we can set the position off our objects is one of the position methods set position, which we can also find under the transform. It is the only want to position for the X value. We don't use a variable. We simply put It's like 10 or 12 to the right. You have to test the bits. It's offscreen, and the Y position is the offsets we just created. The working is variable Now the Officer Fella Visit Institute and position. It's afloat. The playmaker conveniently puts a conversion methods so it can automatically conferred from a teacher without treating intermediate value. Let's see what happens if breast play. The first thing that happens both is that the bulls are positions to the right. The only thing we need to add is having a little move from England. Move. Let the new States call the state. Moving inside this state will translate translate that is the methods and transform. We can translate the object continuously in the X direction, producing a valuable um minus two, which means we have two meters per second to move, actually, and of course, we have to make the transition so we'll add finish state. Once the object is bay placed, it starts movement. That's just protect the bulls out there could start clicking Chopper down. That's basic mechanics out there, but there's still a lot of work. One thing we can notice that the Bulls move, but they never stop. So let's check what we have to do. The Bulls come from the right. They go left, left, left all the way up above, beyond the edge of the screen, at around the position of minus not nine or minus 10. So let's do that. Go to the balls inside the moving state. How we can check the current position gets position is one of the position methods off the owner and what we can do. You can store the exposition Eunuch variable. It's called it current X great variable, calculated every single free. That's very important. Otherwise, we only calculate at the beginning of the book session. What we do will use that federal to compare who's one of the compare functions afloat compared because it floats variable. We compare variable current X we compared with minus 10 and from the moments that's variable is less than the minus time we call the finished state finished state will help us transition to the cleanup states. Of course, this defense is there. It's cold, but it's not used. So let's solve that. Finished. That's also ensure that we do this every single fring on the competitive offering Henry at the New States state called Clean Up. I mean up transition from the finish. Stick to the cleanup states and inside the cleaner states became, Actually Do the housekeeping. Let's use and destroy method to destroy itself. One of the game object methods which effectively cleanups the polls. Let's test it again. Press play can jump sure that we avoid a pause, not just not that important. Okay, we passed the polls. See that movie that move and eventually they get clean up, clean up automatically. That's good. Not to be able to instance, she ate multiple of these balls. We need to make them a prefect. Easy thing to do in unity. It's just tracked your game object into the Essence folder, and you took unity automatically. Replace that with a prefect, which means a reusable object 6. Spawning poles from the game manager: in this section who will at Gabe Manager game measure. For us, it's simply an empty objects. Let's give it to clear name, game manager, and that will help us. Instead, she eight different balls during the game. Let's add the fitted state machine. Let's give the clear named Bull Spawn ER, because that is what it will do on the first status. Make a new poll and we will create an object. Create objects. It's one of the methods for game object. A unity playmaker asks us to get a prefect game. Options can only be insensitive if they are already prefects. So that's what's the reason I had to make a prefect from the ports at the polls as a prefect. On effect, that's all we need to do. That's Dusty's out, well removed ball from the scene. Let the game manager from the book and we'll see. The ball is created clothes, and it gets the same behavior as before, but it's not created by the game managers, so the next step is ensuring that we repeat it every few seconds. So let the New States have a cold, real cool switch between two states state off creating new poll state, where we wait a bit on the right stable coma continuously already learned about the weight state action at the weight action, Let's say every three seconds, real time. And whenever we go to it's finished event that finish transition. At the end of the time, we call the finish event elected. New poll? Yeah, same and finished transition from finished to waiting. So it's like a back and forth between two states creating a poll waiting for the next one. Let's test is out, so we have a ball. Then after a while, the second board arrives. The yield each have their own behavior and our positions differently in the vertical direction. This is already starting to look like an actual key. Paul's gets cleaned up. They are positioned differently every single time. In effect, we can can just continue to play. What is still missing right now is ensuring that whenever we heat ball, that's the Bulls gets cleaned up, and also that the game manager stops spouting books. So at a new state state player horse hit, and that will react on a global transition that we greeted earlier on, the player was hit event Global event. Remember, whenever we hit something that gets sent out? This has the advantage it whenever the player was hit. Global event dissent. We go to the states where nothing happens. The game manager stops and no new balls will be created. That will stop making new polls. But the Bulls, they are there also have to be cleaned up. Let's go to the prefect, selected and introspectively open the prefect because now it's a prefect. We have to ensure that we work on the prefab coat on effect. If you go to look at the cleanup, what we can do is simply at the same global transition for the player was hit custom event . So that way, whenever, um either have the ball moving too far to the left or the player was hit, event is called the Bulls quickly up after themselves. Okay, let's check this out in the game. Stop playing. See? The polls are there. It's one of the Bulls. Everything gets cleaned up and no new polls are created anymore. 7. Keeping the score: to turn our resulted to an actual game. It would be nice to keep a score for debts. Would go back to the game manager, but at an additional officer additional fitted State Machine. This new FSN Let's call it the score manager. So the same game manager object is responsible for both spawning pools and for keeping a score. We call the first state display score for that little at a variable that's making deep variable score Start. It's you we want to display score on screen. So let's go to the actions. Let's go to the Queen Group and we can at a coolie label. Now, unity has different, usually the face systems. This one is easy to integrate inside playmaker, but it is a bit different from the regular, um, mechanism with canvas and the other objects. But this is very easy to integrate for a very basic feedback within the coolie labor, we want to show some text. What we can do is either directly displayed a score or first have a more suitable way to describe it on. We do that by creating a score street. Let's add on a second variable variable of type string. Let's call it s underscore Score. The score is what we calculates. An escort is how we will display its. So before what? We're going to display that variable sing score inside the label and we'll use one of the comfort features, comfort and in teacher to a street. The reason we lose this action is because it allows us to for much the conversion. So the variable we have score variable. It started zero. We converted to the score. The other s underscore score, string variable. But we use a former thing. Be right, score colon and then right. Zero thing is that this zero will be replaced automatically with the value of in teacher. With just a single time, it gets converted and then against displayed on screen. So let's see what happens when we press plea at the top off the screen will see now in a very small form to score being displayed for the score street. What you want to do is increase the phone size of its now. If you look at the goo label, there are no direct means to set any phone size. But the system unity applies is to have a good whiskey. So let's create a new dewy skin. But in the skin, we can configure different things to display. For instance, label which is what we do, what we strike now and here we can indicated phone size. That's put it at 40 on. We can also modifies the normal color. Maybe you can put the font color very bright. Red Advocate, also set in alignment to its centre for upper left doesn't really matter. We also have to ensure that our script the score manager uses that scheme collective actions in any of the reactions we can set degree skin. That's the connection. And we used to go the skin that we just add it political despite globally, so we won't have to do that once. Let's play again and our score is in a bigger from 16 threats. It's much clearer to too few the next thing to do, he said, a new state where we can increase the score. And it's very simple. The FDA in teacher value. You can use one of the actions that's Twitty in Teacher. So the scoring teacher, just simply at one do this once at the finish transition. I go back to displaying the score. Not that it itself not do anything because we have to get in that state somehow, for that's would also use a global event. I mean, create do custom global event. Let's go to the events event this at score. Make it global. And we had a global transition for the escort event onto that state. Whenever somewhere in the game somebody calls the event at score, we will increase the score and go back to displaying that score. Why do we do that? Well, score mention in itself is not responsible for checking the player, checking the polls or checking collisions. It only keeps the score simply receives a message. You have to add the score that is easy to do. Just keep score that's one and displays it on the screen. Other objects are responsible for reacting on events, rectum collisions and, if necessary, send around the event to increase the score. And that's what we'll do with the bulls. Let's go to the polls. Pre Pheap. Click it to open the prefect in a separate cell editor in the same way that we compared the position off the polls with minus 10. Check if we are off screen. We also use afloat Compare float. Compare to check. If we get past the player, the player is always a position zero. So whenever our position reaches minus one, we know we have passed the player during the movie, so you use the same variable current X every frame. I can compare it with minus one the moment it gets less than it doesn't minus one a lot. A new event and post the player. Okay, let's link that if end up. That's what the New States and in that state, what we will do. It's increase score with the same way we use sent event sent, event action, broadcast. All exclude ourselves. And we sent the event. Not a player that was hits but at score events. The global events at score. It's fine. Scores increased. Let's try these out. Collected a seeing past. The first event of scores increased 2nd 1 better. Something strange happening from the moments the pole passes the player. Let's check this. Go back to the Ford Prefect From the moment passes paid, a score has increased, so we saw the scar increasing, but it stops moving That was not what we meant to do. We went outside off the moving state. What you actually want to do is continue the movement. So what we can do will copy. State based it again is called a Moving to states have to be increased to score finished, increasing the score, Get back in the second moving states. Now we have to clear up of its past. The players transition is not needed anymore. I'm a check. We still continue to translate continuously the same way as before we get the position. Every frame. We don't have to compare anymore with the minus one. So variables that action. But we still want to finish our movements average to clean up states. Let's test these out. Okay. Tried to aim between the first poll. It's called increases. Second bowl, of course, to this already starts to look like the extra. Okay, core keeps increasing until we eat one of the polls and they are left. In fact, most of our game is over. Still has to be finished up. At this point, game is almost finished. Now we have to do the clean up and allow the player to restart. That will do in the next picture 8. Finish the game: it's time to rip apart gain and ensure that the player can you start the game. Whenever we heat one of the polls, we come back to the game manager looking, get score. Manager's office. Um, that's a new state. Let's call it the game overstates when we'll really arrived in the game over states. We already saw that with Paul Manager. The Bulls Boehner whenever the player was hit. So we did the same score manager at the global transition from the custom event Player was hit. Game is over. Of course, we wanted still display the score. So let's copy the same way Conversion to string, maybe even just the label, because we already calculated that. Copy the action. Greeleyville based Action. So the score is displayed the same way as before, and maybe we have to at a button. Let's look at a Greek elements green going button. So let's have a bottle. Let's display, restart text. Show me the middle the better now uses the whole screen, and that's the way that, uh, the object is using the screen positioning normalized way. So the left 0% to the left of the top and full with that's increase it a bit. Uh, remove it a bit below something. Let's you know with that may also be smaller. Tonight can also be smaller. Now let's around the numbers a bit left. See 0.3. I don't see your 0.7 with can be 0.4 night. Let's make it a point to That can be our button. But we still want to have to tell what happens when we president but you want to do is every event. Let's make a new event and we say simply recently, start. That's at the transition from for that event. I make a new state restarts game We connect connected to the transition used transition to connect you. Restart event 23 Start getting if you look at level. Um, playmaker provides us with the restart level action. We start left. Let's try. Okay, Is there scored Syria? Whenever it's something score stops. There are no polls anymore. Can we start? We can still see that we can move the player. If you press three starts, the game starts again. Okay, stop playing go better. Our hero heroes is continues to be controllable, so let's also states gave overstates it does nothing. The global transition for our custom events trade was hit. So whenever we hit something, we stop controlling the player. We could even destroy the prayer. If you really want to, that's that's up to you at this again. But we can control the earth, keep a score, score one to never eat something. Still, display the score and to get the restart button. And that's all there is to it. We now have completely function game, very minimalistic. No graphics. Not anything fancy. No sound effects particles. But the game mechanics are there again. Play. We can check the score, and if we had something, displayed the score and allow the players to research the game, and that's it. 9. Conclusion: thank you for making it this far. I hope you enjoy this course and a strongly urge you to make something original from it. Don't just copy flappy birds make something else. Maybe it's not jumpy. Maybe it is running. Maybe it's having different actions, but right now you're all the mechanics. You need to move balls than sure that objects clean up after themselves to react. What? Something that is happening when the player hits it. Baby, you can have have different objects, baby. Other objects will lower your score. Or, if you hit and other objects, he might increase the score twice, maybe have a special effect. Maybe you can even increase the speed after a while so the bulls come quicker at towards you, Maybe a change backgrounds. Maybe you have some color. Some textures do all this fancy stuff, maybe at some particle effects. Sound effects. That's also a good addition. But in this course, we deliberately focused only on the game mechanics to allow you and even a force you to transform them into something original. Something that you create. I hope you enjoyed it. Thank you for watching