Make an Action RPG in Godot 3.2 (P19 | Enemy Soft Collisions + Profiling)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning afternoon or evening wherever and whenever you are my name is Benjamin and welcome to part what is this 19 for a Goodell action RPG series in this video we're going to be solving a problem that we have where if you have two bats that are chasing you like this they can end up overlapping each other and it looks like there's only one bat here because there's no real way for them to collide with each other so we're going to be implementing a soft collision system into this and it's a system that I used back in my game maker days when I was using game maker all the time and it works in Godot as well so we're gonna start by creating a new node Toodee and this is going to be our soft collision component we'll just call it soft collision and we'll give it a collision shape [Music] and we'll save this in overlap we can save it well I guess that's a good spot to save it it makes sense to save it there and we'll attach a script to it it'll be called soft collision Gd and we'll come into here and basically the only thing this needs to do is it needs to detect whether it's overlapping something and then get a push vector so a vector that goes that kind of pushes away from anything that it's colliding with and so we can use that push vector to move our bats outside if they're overlapping each other we can push away they'll push away from each other and move away from each other you [Music] filmmaking is colliding and the way we can tell whether or not this is colliding is if you come into your area 2d right here you can see we have some different methods here and we have a get overlapping areas right here and I get overlapping bodies right here and these return an array so obviously they return all of the areas that you're overlapping with or all of the bodies that you're overlapping with and so we can use that right here to determine if we're colliding with something so VAR areas equals get overlapping areas return areas size is greater than zero so if well we'll get the overlapping areas and this this will be an array and if that arrays size is greater than zero then we're colliding with something so we return true otherwise it will return false now we can make our git push vector function here will save our areas equals get overlapping areas we'll get the overlapping areas again will create a push vector variable here we'll just set it equal to vector zero for now vector 2.0 and then we can check to see if you could call is colliding here again actually and we might might do that we could do that if is colliding here we can do of our area equals areas zero oh we got a oh yeah that's right area zero so what we're gonna do we're gonna get the first area that we're colliding with and we're gonna ignore the others and that actually seems to work pretty well and it helps optimize it a little bit so we're not looping through every single area that we're colliding with we're only worried about one of them and even though we're only getting one of them it it happens often enough that we're able to account for that anyways so if we're if we're colliding with two areas we'll move away from one of them but then they'll also move away from each other and so it works out in the end our push vector will be equal to area global position and that's the area that we're colliding with right it's global position direction to our global position so it's basically we're getting a vector that goes from its position to our position so that's that's um let's open up paint.net here again I can show you so here's us here's them we're going to get this vector right here because that's the direction we want to move in right if we're right here and they're right here and we're overlapping we want to move in this direction to move outside of outside of that collision so that we're no longer overlapping but we want to normalize that push vector so push vector equals push vector dot normalized which once again our vector math here we'll take that vector and make it a length of one whatever that is and then we can multiply it by however fast we want to move away on the other enemy and then we can just return our push vector so tab back here and do return push vector so this will return zero if we're not colliding with anything right we might check is colliding up here so we could actually do this no let's just leave it this way we'll leave it this way okay so now we have our soft collisions let's come into our bat right here come into the bat and we'll add a soft collision to our bat oh the other thing we need to do actually before I forget is come into our project settings and we need to set up a physics layer for soft collisions [Music] that way they don't collide with everything else in the game soft collisions and we'll set it to be both the layer that it's on wait which layer is this soft collisions and the layer that it's looking to it's going to be both okay save and we come into our bat we can instance a soft collision on the bat like this and we need to set up its collision shape and we're going to use right at the base here right down here so we'll do editable children make a new little circle get down so that it's pretty small doesn't have to be very big sighs you really want to and we're gonna actually hide a bunch of our lighters here to get a little bit distracting our editor hide them all and save now we can come into the bat and we can use those functions that we just created in order to move the bat because all those functions do is they get whether or not the bat is colliding with another soft collision and what the collision vector should be if we are so now we actually need to do the logic for moving and we'll do that inside of the bat make an unready VAR soft collisions soft collision [Music] and we'll get the soft collision and then right here right before we move right before we update our velocity we can just do if soft collision dot is colliding so we want to make sure that we're colliding then we say velocity plus equals soft collision get push vector times Delta want to make sure we're play multiplying it by Delta and then multiply it by some value that will make sure that they push outside of each other and the stronger this value it the higher this value is the less likely they are to overlap at all and now we can see if we can get these bets you can see when they get next to each other they don't actually ever overlap with each other they stay outside and we can actually I think it didn't make sense to make our soft collision shape a little bit bigger than it currently is they stay a little bit farther away from each other we'll try that whoops and now you can see they stay pretty far away from each other and if we go into our player here come into our hurt box and we uncheck our lair we shouldn't be able to be damaged by the bats we can get them all overlapping with each other see that in action see they do chase the player and they do push away from each other now this system is it's not it's not really the most I wouldn't I wouldn't recommend using this system if you're gonna have hundreds of enemies on the screen at the same time however I did do a little bit of testing myself to see and if you duplicate a bunch of these I want to show you this because I feel like this is really useful right we get ton of bats on the screen at once I don't know how many this is right but maybe you want a game that has quite a bit of bats okay and save and run the you can see that it seems to work okay right we're not getting huge framedrops here and so it can handle quite a few different enemies at least if you're targeting PC if you're targeting mobile you might want to come up with a different more optimized system and there are ways that you could slightly optimize this by setting a timer potentially so it doesn't check the collision every single frame and other things like that you could look into using a Boyd's system because Boyd's are a pretty common way to handle this and this is similar to a system like that but let's actually look at our performance because I want to show you guys how to do that so if we run the game come down into debugger and if we come into our profile if we come into monitors we can monitor our framerate here and you can see we're holding strong 60 frames per second here it's interesting and my other one it actually shows and I'm gonna on the other screen I'm actually going to attack the bats hit them okay we drop down to 58 per for a second there but it's pretty much holding strong at 60 so you can also come over to your profiler and if you were trying to determine like let's say you were having big framedrops and stuff like that and you wanted to determine where that was happening you would use the profiler for this so what you do for the profiler is you start right here and it profiles the game for a little bit while like this then you would and you could choose which things you wanted to profile here and see our get push vector is included in here and then you would stop and you would get data and you can go to any frame here and you can actually look to see what functions and what areas are costing the most amount of time so you can see that our [Music] let's see if we can find here's our git push vector right here and is colliding you can see that they are having a lot of calls right here but we're we're still doing okay with our with our total time here so this is if you were having big issues with optimization this is where you would figure out why and where that is happening so like what if we turned off the physics frame and we said okay let's check our get get push vector and see you'll you'll obviously see that if we start this here and we have a whole bunch of enemies we're gonna get a lot of stuff but then over here on the Left I'm gonna kill the enemies and it's gonna drop down because we're not going to have as many things actually attacking well it didn't seem to make too much of a difference actually so well it's not even calling it at all now I don't think see oh it did go up a little bit there obviously I had I had I'd kill all the bats and then made these to start interacting with each other again when I move the screen we get drops that's interesting that moving this around gives us a frame drop but that's probably normal anyways this profiler is how you would determine where you're having issues in your game actually this is less time I think that it's taking to call those functions I understand this properly I do wish there was some sort of an indicator over here on the side as to what these actually mean like is this how many calls there are what what what is this number here I'm not actually sure now that I'm looking at it it makes a lot more sense with the frame type I'm guessing this is the milliseconds yeah it's up here frame time in seconds right here so that's what this is showing right here anyways there you learned about monitoring so you can monitor your frame rate and stuff and your process your physics process you can see where things what is taking time and you learned about your profiler so you can actually go in and profile those things we're gonna get rid of a bunch of these bats we do not need this many bats [Music] and will move or move this one oops the world move this one so it's not so it doesn't attack us somebody mentioned that they're like you should move that bat so it doesn't attack you every time you start the game but yeah now we've got a good system set up for our bats so that we can handle overlap and it does pretty well handling a lot of enemies on the screen and that's going to be it for this video let me make sure that we set our players hurt box back to be the proper collision layer that way we can take damage from the bats again I hope you guys enjoyed this video is made possible well if you don't know who is made possible by at this point nineteen videos in you should already know and if you're interested in my one that ghetto of course you can check that out there'll be a link in the description a link at the end of this video I appreciate all your support if you enjoyed the video give it a like subscribe to my channel and I will talk to you all later
Info
Channel: Heartbeast
Views: 56,214
Rating: undefined out of 5
Keywords: Godot Engine, Godot 3.2, Godot 3, Tutorial, Series, Action RPG, RPG, Pixel Art Game, Indie Game, Game Development, Learn gamedev, Gamedev
Id: ffXx0dPejWY
Channel Id: undefined
Length: 16min 56sec (1016 seconds)
Published: Wed Apr 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.