How to Make a Game with Visual Scripting (E02) - Gameplay Loop - Unity 2021 Tutorial (Bolt)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
You made it to part 2 of this mini-series and in this part, we going to create the gameplay loop for this game this game is going to be level-based so we need to add a way to win the level and a way to lose the level also in this video we'll add the ability of picking up coins so let's get started the first thing that we're going to add is the ability of losing the level and the two possible ways that you'll be able to lose in this game is either if this cube falls off or if we collide with an obstacle the way we can check if the player is falling down is by checking the y-axis of our player so let's go to our graph i'll remove start and update we're not going to be using those units in this graph before we add a check if we're falling down let's add this graph in a group and we can do that by holding ctrl on the keyboard and dragging that selecting these units right here and these units are gonna be our movement after we finish the logic with the movement i want to do that check if the y is less than zero so i can drag out a connection and we can look for an if unit and the if unit accepts a boolean input so it true or false and based on the value we can choose what we want to do so let's create that boolean input and we can search for the transform component and we want to get the position of our cube we just want to look at the y-axis so let's get y of a vector 3 and now we want to check if this y value is less than negative 1. so we can use this less than unit add that in and for the b value let's pass in negative one and this less than unit gives us the boolean and we can connect it to our if unit let's add another group and i'll just call it if falling if we're falling off the platform what i want to do is actually restart the game and the way that we can do that is by loading a scene we can find that under scene manager load scene and i'm going to select this unit right here load scene by scene name you could just pass in the scene name that we use right here sample scene and it's going to load the scene but when we're going to add more scenes for the levels this won't restart the current level so to make it restart the current scene we can again use the scene manager and get active scene this gives us a scene object and from this in object we can get the name and pass that name as the scene that we want to reload so let's move this and let's name it restart scene let's go and test it out so now if we fall off the platform you can see that the scene actually restarts and we do have this different lighting for our scene and this only affects the editor if you export it it's not going to have the problem but to show it correctly in the editor as well what we can do is go to window rendering lighting create a new lighting settings and right there we get a new light settings file so just name it as you want and we want to generate the lighting click generate it's going to go through and generate the lighting and now when we play you can see that the lighting is the same when we start the game now let's also add the logic of losing when we hit an obstacle and to do that we can go back to our graph and here we can look for uncollision and the options we have for on collision is exit stay or enter there's also the 2d versions of it but we're doing a 3d game the one we're interested in right now is on collision inter so whenever we collide with an object this event is going to be triggered and if you want to see which objects you're colliding with we can use a debug log unit and pass in a message there one of the output values that we have from uncollision inter unit is collider and that is the collider of the object that we collided with so what we can do is get name of that component component get name is the unit that we want to use and pass that as the message for our debug log and now when we click play if we go to the console you can see that we collided with ground which is correct because our cube is on the ground and then a little bit later we collided with an obstacle now we want to lose the game only if we collide with an obstacle so we need a way to filter out that collision we could use the name of the object to filter it out but at any point if you rename that object our logic is going to stop working so the better way of doing it is actually using the tags and you can find the tags in the inspector right here currently it's untagged we can look at the options that we currently have if we want to create another tag we can click add tag and add another tag so i want to add a tag called enemy click save then we need to select the obstacle again and now in this drop down menu we can find enemy so select that and our obstacle has the enemy tag now let's go back to our player graph we'll remove this debug log component and now we can create another if statement here that will be checking if the collector has the tag enemy let's drag this value out and search for compare tag this is the unit that we're gonna use so add that in and this unit returns a boolean so we can pass that to our if statement for the tag we're gonna put enemy and if we collide with an enemy we're gonna restart the scene just like we did when we were falling so we can reuse this group that we have here let's put this in a group as well we'll just name it if collide with enemy and let's go and test it out and now whenever we collide with the obstacle you can see that the level restarts but instead of restarting right away i want a little delay so we can actually see the collision let's go back to the graph one of the ways we can do it is by adding a timer node between these units so let's go ahead and do that from true we're gonna add a timer and on complete we're gonna restart the scene the duration is currently set to one second so let's leave it at that but there is a slight problem with this setup currently we won't have a problem with it but if we collide with another enemy within one second then the timer is actually going to restart because whenever we trigger the start for the second time it actually restarts the timer and that's not what we're looking for the way we can prevent that from happening is by using a once node then the ones node is pretty straightforward whenever we trigger this node for the first time it's going to go through the once flow and afterwards it's going to go through the after you also have a reset option for the once note if you need to do that so let's put all of that into our group let's go and test it out so now you can see that we get a collision with it and one second afterward the game restarts now one more thing that i want to do is after we collide with that obstacle for the movement logic to be turned off and to do that let's go to our graph once more and right here in our movement logic we can add another unit here that we'll be able to use to disable this logic so under control we can look for the toggle flow and in the toggle flow we have an on state and an off state whenever the toggle flow is gonna be on we're gonna be doing the movement logic but if our flow is gonna be in off state then the movement logic is gonna stop to switch the toggle flow into off we're gonna use the timer started output and connect it to off so now whenever you collide with obstacle you can actually feel that you lost control over that cube and that's the feeling that we're trying to go for now that we covered both cases of us losing let's cover the case where we actually win in the hierarchy let's add another 3d object let's use a cube this is gonna be our finish so let's position it at zero and one let's add another material for it we'll choose a green color for material add that in and we can move it to the end of our level scale it up in x axis to 15 and let's select the finish tag for it we can go back to our player added graph and in here on collision enter after we compare with an enemy and we get on false i want to check for another tag to speed things up a little bit what you can do is select these two units that you want to duplicate and click ctrl d on your keyboard that's going to duplicate those units for you and now we can connect the if unit to the false output of the other one and also connect the collider to the compare tag change the tag name to finish because that's what we're checking for if we collide with the finish line what i want to do is actually stop our cube from moving and since we're using rigid body to control our player what we can do is set kinematic of our rigid body to true and that will stop our player from moving or bouncing off the finish line since we're gonna take a look at the logic of loading at the next level in the next part for now i'm gonna use the restart logic right here and connect it there we can test it out now so now if we reach the finish line the cube is going to stop and the game is going to restart the last thing that we're going to do in this part is add coins so let's add another 3d object let's use cylinder this time move it up let's scale it to 0.2 in a y-axis and rotate it by 90 degrees in the x-axis rename it to coin let's add another material i'll name it coin material and for the color let's choose a yellow color add that to the coin so now we have a yellow coin select this coin and for this coin i want to do is something different with the collider that we have here so we have a capsule collider here just going to leave it as is but i want to switch it to be a trigger so that our cube won't actually collide with it and i think the scale needs to be a little bit smaller so let's set it to 0.8 in x 0.1 in y and 0.8 in z now let's add the coin logic to our coin object so let's add a script machine we'll use the embedded graph but before we go and edit the graph let's select our player and go to inspector select a player tag for a player select a coin and go edit graph we can remove the start and update events and now instead of adding on collision event we want to add on trigger event we'll do the same check so add an if unit and let's add a compare tag unit connect that to the if unit and the tag name that we're comparing to is player if this event was triggered by a player then what we want to do is add a coin to our counter and then remove this coin now there's several ways you can add a counter you can add a counter as a scene variable and then after you finish the level you can add those point to a saved variable which is going to be the total coins you receive or you can just count the total coins i'm just going to count the total coins so i'm going to use a saved variable let's initialize the variable coins and this is going to be an integer set to zero we can drag this coin variable into our scene and now if the player triggers this event we want to add one to this coin variable so let's look for add we'll use the scalar the value for b is gonna be one and now we need to save this variable back to coins you can search for saved variable and add it that way the other way you can do it is by dragging the variable from the list to get the set option you can hold alt down release it and that's going to give you the set variable connect that on true and pass in the incremented value the last thing we're going to do is destroy this game object so we can look for game object destroy and for the object input we want to pass in this because we want to destroy this game object with that our logic is complete let's click play so now when we collide with a coin that coin gets destroyed and our coin count increases to check if the coin count increases we can go back to our graph and check saved variables and then instead of the initial we can check for saved and the current count is at five in the next part we're going to look at ui and how we can display that coin count and also how to create levels if you like the video click on the like button if you have any questions write in the comments and i'll see you in the next
Info
Channel: Smart Penguins - GameDev
Views: 14,472
Rating: undefined out of 5
Keywords: Tutorials, GameDev, Smart Penguins, unity, unity game dev, indie gamedev, indie game devlog, game devblog, how to make a game, how to, brackeys, game making, game program, game programming, indie game, video game, easy, course, game, development, develop, programming, coding, basic, make a, introduction, visual scripting, making games, getting started, unity bolt, How to Make a Game with no code, unity visual scripting tutorial, visual scripting tutorial, unity 2021 tutorial, no code
Id: Ani-xBl5DNs
Channel Id: undefined
Length: 11min 31sec (691 seconds)
Published: Mon Apr 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.