Godot 3.1: Creating a Simple 3D Game: Part 7 (Enemies, Animation, Collision, Game Over) #GodotEngine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hey I love your blender tutorial series. I'm watching the 2.7 playlist currently. Thanks for the good work.

👍︎︎ 2 👤︎︎ u/[deleted] 📅︎︎ Jul 07 2019 🗫︎ replies

This looks great. Thank you.

👍︎︎ 1 👤︎︎ u/phaseforty 📅︎︎ Jul 07 2019 🗫︎ replies
Captions
hello and welcome art 7 of creating a simple 3d moving dodge game using the Godot game engine my name is Colin and in this story all series we're creating this simple 3d movement dodge game where you are a ball you move around a three-dimensional maze you avoid hitting enemies you collect coins if you collect all the coins you win the game if you accidentally hit an enemy you go to the game over screen and you have to start all over again in this video we're going to go over how to create enemies that are animated in the level so let's go ahead and break this video down number one we're gonna be creating an enemy scene from a model that we're bringing in from blender 2.8 second we're gonna animate that enemy yep that's pretty straightforward but enemies are kind of useless unless something actually happens or we have some place to go if you hit the enemy so we're gonna create a very quick game over screen but we did go over how to create simple menus in the last tutorial in this miniseries so I'll be going over that quite quickly next up we're gonna program collision detection so that our enemy knows when it's hit our character and if that collision is detected we can send our player to the game over screen for them to go ahead and try again and last but not least we're going to add more enemies this might sound redundant but it can be a little bit tricky to animate and have different animations on different enemies so we'll cover that in the last part of this video of course if you have not seen the first six videos in this mini series on how to create a simple 3d moving dodge game I'll put a link to this playlist on the screen right now and of course if you like this video or for learn something go ahead and click on that like button below this video on YouTube it really helps me and my channel out and if you want to see more videos like this one in Godot or blender or technology click on that subscribe button and click on the Bell icon to get notified whenever I upload a new tutorial we're gonna start off in the Godot game engine just to recap what we did in the last video in the last video we made a simple menu with some text that uses a font and a button that we can actually click to switch us between this screen and the level seen in our game between the last video and this video I did something that is um that I was going to do I adjust it the camera and where it is and what I can see in the game because it wasn't quite optimal and I changed the speed of the ball so just very quickly if I select Steve's our character our ball Steve's camera target and I'm kind of orbit around you can see it's right here I changed its position and exhortation so I moved it around and then I went and I played my scene and I looked and son saw if I liked where the camera was and if it was framed nicely I also took the camera this interpolated camera and I adjusted the field of view this fov value in other words how wide-angle or how zoomed in the camera is the cameras way back there right now so a larger field of view means a wider camera angle so now if I go ahead and play this scene you can see that we can see our ball in the middle of the level and the ball stays nicely kind of in the middle of the screen roughly or I think it's pretty good so the camera is a little bit lower and it can see more all at the same time the other thing I did was I sped the ball up so if I go to Steve and I look at Steve's script file all I had to do to make Steve move faster or change the values of these two constants speed and rotation speed and I just turned those two numbers up because of course if you make your character move faster because it's a ball it has to roll faster as well I believe these numbers were something like five and six or six and seven before so I basically doubled the speed of the ball and I like that let's go ahead and get an enemy from one or two point eight into good dough so I'm gonna minimize I'll go back to my 3d view and then minimize Godot and in blender 2.8 this is my rather not so creative enemy it's a little spiky ball kind of themed after one of those massage medicinal balls that you roll your foot honor you're backing your role in the floor it has a material although right now in there are 2.8 I'm in solid view up here so if I switch into look dev you it's got a shiny purple material on it and I also have everything in this scene selected so I've got the camera and the lamp they're two they're selected so I'm gonna just right click or actually pardon me left click on the ball to select it in about a two point eight and then I want to get this to a gltf file so we're gonna go to export so file and export and with that one object selected I'm going to go to gltf 2.0 and when we do that I'm gonna go to my desktop and then go into my project folder go to a basic move in dodge game and then if I go into imports here I'm gonna save my gltf of the enemy object and by the way before I do this I need to make sure that my enemy is named so I might want to go with it selected to the object tab and make sure I'm named the object which I have and I want to make sure that its material is named something that's not just material I caught myself so I'm gonna name this enemy - material and hopefully I can not make a typo while I'm typing that and I'll press Enter so we do this because Godot will get confused between materials if we have two materials from different objects mean the same thing okay really quickly file and export gltf 2.0 and I'm gonna go to my project folder on my desktop and go into imports and enemy and I need to change a couple settings down here under my export settings I need to make sure I'm exporting as a gltf embedded so not a binary but an embedded and I want to make sure that I use only selected objects so it'll only export the ball and not the camera and the lamp okay export gltf 2.0 it'll just take a sec and it should be done so now if I open up good--do it will quick we import it and now we've got it in our imports folder as enemy TLT yeah I can double click on it and it's saying I need to make a new scene and so I'm gonna say not the default I'm gonna say open anyway that's important I find this easier to work with once you have this new enemy scene with the purple spiky enemy ball I'm going to save this scene so scene save scene as because I'm saving it as a new scene essentially and I'm in the imports folder so I'm gonna go up one with this little up arrow and I'm gonna go into assets and I'm gonna main this enemy dot es en that works for me I'll click on save now that we have our own actual Godot scene file I can edit it okay so this scene route I don't want it to be a spatial node that's what that is right now so I can actually right click on it and I'm gonna change its type our enemy is actually going to be a new kind of an object we don't need our enemy to be a physics object at all actually we just need to be an area an area is under spatial and I might not be able to there it is under collision object I don't know if I'd be able to find it or not there it is an area will not bounce or hit or be able to push or interact in a physic Sikandar way with other objects in your scene and that's okay for an enemy because an enemy doesn't do any of those things we want me to bump into our enemy we just need to know if we are coming into contact with our enemy so what once we - we're gonna switch scenes anyway so there's no physics involved here but it can detect collisions and areas are really good at detecting collisions so area and I'm gonna change this type so now we have this scene route which I will actually call actually I'll rename my enemy mesh into enemy mesh and then this area will be called enemy okay it's giving me a little error and you know what that's about it says this node has no shape so it can't collide or interact with other objects consider adding a collision shape or collision polygon as a child to define its shape you've seen this before we need to add a collision shape node as a child of this object even though it's not a physics object we need to define its size or area or shape so with enemy selected not the mesh but the area I'll press + we're gonna just search for collision shape and there it is the pink one not any of the two the blue one so collision shape and this one's giving me an error because it needs to know its shape so with collision shapes selected I'm gonna go over to shape and say hey it's not it's a sphere so I'm gonna say new where is a new sphere shape and it's too small or is it let's go into orthogonal view and let's go into front view and let's see how we're doing here yes I believe it was too small so I can drag the little pink handle out or I can look at its properties if I click on sphere shape its radius I believe it needs to be - yeah that's the right shape because we caught it from blender and it was basically made from a default cube so hey it's a radius of 2 so it's 4 across ok so hopefully that will work on there - a ctrl s to save this enemy scene I don't know back into my level and I'm actually first thing that I'll do is I'm gonna delete this box because it's not actually gonna be a part of my final game sorry to see you go but I'm gonna grab the box one and I'm gonna right-click and say delete nodes and yeah box is gone because we're gonna select the level and I'm not gonna press the plus I'm gonna press the little link button we're gonna make instances of our enemy so I'm gonna press a little link button and where is it it should be in assets and and dotty Sen that's our scene file that we actually customized in Godot not our gltf enemy okay that's important so I'm gonna press open and there is the enemy and you know what it's way too big so I thought that might happen we can actually go back because this is instanced any change that I make to the original enemy scene will get propagated back into any instances of it so if I go to the enemy scene and I go to my enemy mesh and I open up its transform properties and its scale asset to one one on one I can change that to 0.5 0.5 and 0.5 and now the ball is yeah it's over whole width so it's quite a lot smaller and volume and now it's collision shape the sphere is way too big so I'm gonna change this radius to one over in its inspector properties okay if I do a quick control s and then I go back to my level you can see the ball is now the right size so I can move it up perhaps and I might go to my orthogonal front view and I might move it around and I might have to orbit and see where I think it belongs you kind of just have to eyeball this or you could look at your transform properties and I think one might actually be the right height but it can be hard to tell so I'm gonna put this enemy where I think it should go we're gonna have multiple enemies but let's go in orthogonal view let's go up to our top view and I'm gonna put my first enemy oh let's say we're gonna animate in an l-shape kind of moving I'm gonna use the Move tool it's gonna animate kind of starting right about there and going to there and then going down and then doing that you know nice little l-shaped loop we're not gonna get much more complicated or simple than that in this video so I'm gonna start it off right here and we have to animate it how do you do that well to our enemy we're gonna add a child node called an animation player so I'm gonna select enemy in fact I'm gonna rename him enemy 1 and press Enter I'm gonna press with him selected the plus button and I'm gonna look for the node and let's see if I can find it without searching called animation player it's just right there under node so I'm gonna select it press create animation player is now a child of enemy 1 and when you have an animation player on a node you actually get a new bottom panel down here called animation and you can name an animation once you make it but this area down here that's empty right now is essentially gonna be filled with tracks with keyframes on it over a timeline and what are we animating well you can actually animate if you select an animation player that belongs to a node you can actually animate any of its parents properties now first off though we actually have to make a new animation so I'm gonna click on this word animation which is actually a button for a menu and I'm gonna say new and I'm gonna name this animation guess what enemy 1 animation it's a very creative name I know so I'm gonna press ok and once I do that I get what looks kind of like a timeline and now if I select my enemy look what I have I have all these little key icons next to most of the properties in my game including under the transform category over here the translation which means movement I have got a little key there and rotation which we're also going to use to roll the little spiky balls around so the idea here is that you create tracks for animation only on the properties that you want to animate so with my enemy one selected I'm gonna press little key next to translation and it says create new track for property translation and insert a key yes so I'm gonna press create and when I did that down at the bottom you can see a translation track and a keyframe this is a lot like blender if you're familiar with blender and its timeline over on our 2.8 it's new timeline with keyframes that you can actually move which look like diamonds which is a lot like blender 2.7 dopesheet editor window okay so if i go later in time just by dragging this little playhead bar and the weird thing about kudo is that you can only drag it in this top section that has numbers in it you can't drag it anywhere down here that's a little quirk of Godot our timeline right now though is only one second so we can change that over on the far right in that window there is a one here and that's the duration of my animation so I'm gonna say well let's think about this my ball is going to go or the enemy is gonna go probably in about a second and a half it's going to get to here and then it's going to move down in probably two seconds down to here so that's 3.5 seconds so then we need 3.5 seconds to get back because we want to create a loop so that's seven seconds altogether three point five plus three point five okay so I'm gonna make this animation be seven seconds so I'll type seven of them there press Enter it looks like it's grayed out here but that's just the active area and you can only drag your playhead in that area so now I'm gonna move it drag my playhead to one point five seconds it's measured in seconds and I'm going to move my enemy to right about there and I need to insert a keyframe so I'm gonna press little key button next to my property so I'll press those key button and I've got a keyframe there so now I've got animation let's keep going I'm gonna go to three-and-a-half and I'm gonna move my enemy to right about there and I'm going to press little key button next to translation don't press one of the wrong key buttons that's important and so now we've got an animation in an L shape and now what I can do is I can actually duplicate this keyframe just by clicking on it and if I put my playhead where I want that keyframe to go so of five and a half seconds I can actually I believe just right-click on it and say duplicate keys and it'll put the duplicated copy where your playhead is so right click duplicate keys and there we go so now it goes back up to that spot and again I'm going to duplicate this one at the beginning so I'll click on it and then I'll put my playhead at the last frame at seven seconds if I right-click on this keyframe it's a little bit tricky because you can't right-click over by its name so get a right-click rape on the edge of it and say duplicate keys so now we have an animation of the ball moving rotation works the same way although we have to kind of guesstimate how much the ball will rotate so I'm going to select my enemy and go to rotation degrees and click the little key icon I'm at frame or the zero seconds on my timeline so I'm going to press rotation degrees that little key and yes create and now this is where I might want to orbit into a view where I can see what I'm doing here we are rotated in this way on this keyframe I'm gonna go to where it gets that corner and it will have rolled so at this point in time I'm going to grab my rotation tool and if I rotate it on the z-axis this is where you can see how many degrees I'm rotating and I can just type in here as well so I'm gonna guess that it's gonna rotate mmm 270 degrees and I'm gonna press the key button and now I'm gonna guess and check so how I guessed and is it rolling in the right way I think it kind of is so I'm not going to be too accurate for this this is our first project so I'll just leave it at that it rolls a longer distance so I might go to here and I'm rotating now on the red x-axis so I'm gonna rotate it just a little bit so I can see which one I'm gonna do anything to use this one here I'm gonna say 400 degrees that way okay I'm gonna press the little key button there so now if I scrub through it looks like it's doing the right thing okay and the same idea here I'm gonna roll in the opposite direction so I'm gonna select this keyframe go to five and a half and then right-click on it and say duplicate and then I'll go to seven seconds and select this one and say right click duplicate keys and so now I have my enemies animation let's go ahead and see what that looks like in our game right now I'm looking at my seen through orthographic view or orthogonal so I'll go back into perspective just so I can see that it does have a shadow and it's floating a little bit that might be a problem I have to go back and revisit all my keyframes and move it down a little bit on each one but I'm not gonna worry about that for the sake of the length of this video so let's go ahead and see what this looks like I'll do a ctrl s to save and then play a scene and as you can see it's not animating because we haven't actually told the enemy to animate or actually play its animation so I'm gonna close my preview and select my enemies animation player what I have to do here is first check or press this little circular a button which means loop the animation because we want to how the animation play over and over and over and over again next up we have to make this action here enemy 1 animation I use the word action that's a blender word but this animation play by default right when the game first Loews to do that I'm gonna press this little a plus tag looking button and if it turns blue that means it's set to be true and this is the autoplay option you can do this with code as well although this is the easier way so now if I do a quick ctrl s to save and play this scene you can see that now we have an enemy and it's rolling and is really not rolling in quite the way I was hoping so I'm gonna pause the video here and see why that's happening because it's obviously twisting in a way that I don't like there all right so I think I figure it out and let's go ahead and fix this problem together I went and I selected my animation player so I can see my enemy one animation and if I look at the enemy and it's translation specifically its rotation degrees field right here with XY and Z and if I look at what's changing here when it rotates from here over to here it's rotating on the z axis so if I go and watch this value and if I kind of scrub along my timeline when I get to this point it's rotated this way the 270 degrees now when it rotates from this point to its final point before it resumes and does the animation in Reverse it's rotating now on the x axis you can see this value over here is increasing all the way to 400 but for some reason and I'm not sure exactly why the Z value is changing when it shouldn't so it's it's rolling almost back a whole bunch and even beyond the zero it's going to negative 90 when it should stay at 270 so I'm gonna just go to this middle keyframe and where it's right at the end now I'm gonna change us value manually to 270 and I'm gonna press the little key button so now if I watch these values yeah it's staying at 270 degrees from one and a half seconds in all the way to three and a half seconds in and then it's staying to 270 all the way until gets back to this point which is exactly what I want to do but it is rotating on the X between one and a half seconds to three and a half seconds and then it's rotating in the opposite direction to go back from 400 all the way back to zero here so I'm not sure why I had to do that but I think this will solve it and then from five and a half to seven seconds it should rotate or the z-rotation should go back down to zero so let's go ahead and save this scene and play the scene and see what it looks like and it seems to look pretty good and it's playing and it's looping so now we have to get to actually hurt us because I can go through this embrace not a collision or it's not a physics object so it's not gonna bump into me it's not gonna push me along and I can't push it so we're gonna enable collision detection let's go ahead and add that one of the nice things about an area node which is the basis or root node of our enemy scene is that if I select the enemy over here in the scene dock and then go over to the node tab beside the inspector and look at the available signals that are available on an area you can see that each node type has its own category and different nodes like area have different signals or default event handler signals that are specialized for that kind of a node and areas are really good at detecting when they are entered by a physics body like our character a kinematic body the area is going to detect when a body enters it so I'm gonna actually with my enemy selected under the node tab under signals I'm going to double click on body entered which is going to now ask me to point to a code file where I'm gonna have to make a function that handles whenever this signal is triggered and is fired or sent out when a physics object like our character enters this area now in the last video of we use signals like this in order to make a button switch us from one scene our menu back into our level and in that case we actually gave the button its own script file and so the button actually handled its own event or its own signal in this case our enemy is going to send a message to our character Steve so I'm gonna select Steve and on Steve's code and Steve does have a script file in his script file I'm gonna make a new function or a method called on enemy body entered so I'm actually gonna get rid of this enemy one part the one part so just leave it at enemy because we're gonna reuse this for every enemy in our scene and I'm gonna press connect so Steve is selected I changed this method name here I'm gonna press connect and when I do that we've now been jumped over onto Steve's code I can click on this little icon there and you can see that's where we are and if I look at the bottom of our code here it is function on enemy a body entered and it's given us this little temporary passed line which I need to replace to actually do something and if I look at my enemy it now has a little signal icon and if I have it selected in my scene dock you can see that it is being handled this body entry is being handled by Steve and it's calling a method or function called on enemy body entered so now we know it it works the way we want now what are we actually gonna do if we come into collision with our enemy well we need something to happen and for now I'm just gonna use a test method called print' to just make sure that everything is working the way that we want and then I'm gonna make a very quick game over screen so on Steve's script I'm gonna replace this past line with a method call to print and this is not a function that you can use when you export a game it's just for testing and what you can do here is you can put double quotes in this method calls round brackets and I'm gonna put hit okay I think you might have seen this in the very first video in which I talked about programming this print method is built-in to Godot and you can call it to print something or text down in the bottom output window while you're testing a game or play a game just from the editor here so let's go ahead and play this seam and hopefully if I hit an enemy it will say hit down with an exclamation mark down in the output dock at the bottom okay let's go and see what's happening here huh we're getting lots of hits because uh this enemy is hitting other things I believe you might be hitting the wall or the floor so we need to be a little bit more specific so when the enemy has a body enter it I'm gonna check to see if the body's name is Steve because we have named our character is Steve and that way it won't give us a false hit print out if that's hitting something else so I'm gonna write an if statement so I'm gonna press ENTER to move that one line down and I'm gonna write if body oops body dot name is equal equal to in quotes I'm gonna put Steve because that's the name of this note that I've given it in the scene doc and I'm gonna put a colon at the end of that line and now I'm gonna indent this next line because it's gonna be the result of this if statement so I'm gonna put my cursor there and press tab once okay let's go and see if that works I'll do a ctrl s to save and we'll play this scene and now you see that we're not getting that he it happen automatically but I'll have a roll my ball into the area you're seeing that every time I move my ball into the enemy I'm getting a hit if I roll in once more it's gonna detect I hit okay so it's working exactly the way they want is detecting a physics body entering it and it's only gonna do something when that object happens to be Steve so let's go ahead and very quickly make a new screen a new menu so I'm gonna press this little plus button we're gonna go back into 2d view I'm gonna make a user interface a scene and so a control node [Music] see it and if I click on the play again button it should take me back to the menu which does and now I can play my game now we just have to connect up hitting the enemy to go to the game over screen I'm gonna let this load my level though there we go okay so now it's getting us there but it's not actually taking us to game over it's printing hits down here so last but not least almost we're gonna go to our level and look at my Steve code because Steve handles his own collision with the enemy I also I'll click on my script file and instead of printing hit here guess what I'm gonna do I'm gonna get the tree and change the scene it's the same code as the button or in in or any of the buttons that we've made before so get underscore tree and then a dot and change underscore scene and round brackets and I'm gonna change to game over so I can just click on my game over on the bottom left and then copy this path control C and paste it in here and I think we are almost good to go but I think it will work let's go back to our in fact you can pause the video if you need to get that but there it is let's go ahead and try this scene out there we are so now if I roll into the enemy I go to the game over screen I can play again I can go back and press play on the menu and so now I'm in my level again if I hit it again I quote a game over I think you get the point if I don't hit the enemy I won't go to game over so we've got one enemy and this is the last part in this video I'm gonna duplicate my enemy and it's a reason why I named it I think enemy 1 it's default or root node is called enemy so on our level it's enemy 1 if I right-click on any one and I say duplicate I now get enemy - the problem is this animation player or enemy 2's animation player has this enemy1 animation in it and the reason why we didn't put this animation player in our enemy scene is exactly for this reason because we want to have a different animation play on each individual copy or instance of the enemy so I wouldn't be able to access it as easily if I had put it in the enemy scene because I would be able to click on it here in my scene dock okay so that explains why I didn't do it that way I'm gonna actually go to an enemy to animation player I'm gonna click on the word animation I'm gonna say new and I'm gonna name this one surprise surprise enemy to animation and press ok we've got a new animation and I'm gonna move enemy 2 which is in the exact same spot as enemy 1 I can go back to my top view and then orthogonal and let's move the enemy where do we want this one it's really up to you I might make one that simply goes back and forth here it's really up to you so again I can put it where I want to start and then click on enemy 2 under the inspector and I can change the transform keyframes by inserting a track with the little key button so I'm going to insert a keyframe of it right there yes I will and that's that one second in effect I'm gonna move that keyframe just by dragging it down to zero this is gonna be a little bit of a different animation I'm gonna have a it pause here and then it's going to move and then pause here and then move and then I can repeat that pause so this actions gonna be one second of pause one second of motion one second of pause over here and then one second of motion so that's four seconds altogether so I'll change my value over here this actions gonna do auto play and it's gonna loop and I'm going to make it stay there for a second so I'm just gonna duplicate this keyframe right-click duplicate keys so this line means it's staying still if you're familiar with blenders dopesheet window you'll be familiar with something like that and now I'll have it move so at two seconds it's going to move over to here and I need to get my enemies I'll click on my enemies and click on its key next to translation and now I'm gonna have it stay there for a moment so I'm going to duplicate that at three so I'll say right click duplicate keys and then it actually wants to snake back for some reason to its original position I'm not sure why that is I think that's why we had the problem before where it rotated in a funny way so I'm gonna go back to four and just insert a duplicate of the first or second keyframe there so right click and duplicate keys and now it actually knows to go back there properly with its own actual key frame so I'm not gonna bother doing the rotation animation for this one but let's go back to perspective so we can see our lighting and shadows do a quick control s and the only remaining thing that we need to consider is does this enemy collide let's go ahead and try that out I'm gonna press play the scene and here we are in our game you can see my enemies moving back and forth it's not spinning or rotating but we can add that later I can happily move around my level but if I happen to bump into that enemy yes it will go to the game over screen because and I can go back to the menu and I could play again but it's working because it still has a signal that if I were to click on that little icon it takes me over to my node tab and the signals and you can see when a body enters this copy because I duplicated it it connects to the same on enemy body entered method or function on my Steve and so it works and if I look at my Steve script it still checks to see if the body that the text is named Steve and it changes the scenes so it all works as you would expect so that we'll be it for this video of course if you like this video over learn something go ahead and click on that like button close video on YouTube it really helps me in my channel out and if you want to see more videos like this one in Godot or a blender or technology click on that subscribe button and click on the bell icon to get notified whenever I upload a new tutorial check out my facebook page at facebook.com/cnn sneak peeks and I communicate with you my viewers the most there on my Facebook page but that will be it for this video thanks for watching [Music] you
Info
Channel: BornCG
Views: 37,327
Rating: undefined out of 5
Keywords: tutorial, lesson, 3D, model, modeling, modelling, beginner, godot, godotengine, engine, game, game engine, gamedev, dev, programming, code, coding, gdscript
Id: R_dQdKQyF_U
Channel Id: undefined
Length: 38min 12sec (2292 seconds)
Published: Sat Jul 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.