Make a 3D Top Down Shooter with Godot - Part 3.2 Gravity and Falling into the Void

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello folks welcome to the age of asparagus and part 3.2 of making a 3d top-down shooter in cadeau by the end of this video we won't be able to cheat anymore by going off the edge of the map instead we'll fall into a black void to our death we'll also make our character's movement a lot smoother so let's start by adding some gravity we'll go to our player script and i'm going to keep the y axis velocity logically separate from the x and z which is the player controlled movement so let's add a we'll add a constant gravity value here and i'm not really sure what it'll be let's just set it to something like 10 for now and we can adjust that later and down here when we move in slide instead of moving at our velocity which has an x and a z value we're going to insert a y value which we'll call a following speed and i'm just going to define that up here we'll set that to zero to start so the falling speed each frame will be minus we'll subtract gravity so it'll decrement it'll reduce by 10 every frame now the thing is once we incorporate it the move and slide method it's gonna our player is gonna collide with the floor and it will keep resetting to zero basically because of that collision and it's only when it goes off the edge that gravity will start subtracting and subtracting from the falling speed so if we're going to insert the falling speed instead of going velocity we're going to move and slide by a new vector 3 and we'll use velocity.x for x but we're going to use the falling speed for y and then for the z value we'll again use velocity the z component let's close up that bracket here now okay let's just see what that does um i can still move around good we always want to make sure i don't kill the movement and what happens if i go off the edge now craziness okay um and the reason for that is because falling speed doesn't actually get updated by the move and slide method we we insert it but we don't actually get a result back so move and slide returns the velocity of the node the object of the body um after it runs we're not using it because right now we always just reset the velocity back to zero we don't have any kind of nice move but for falling speed we do want to know what the result is after the move and slide so let's just create a new variable called result and we'll set it equal to the move and slide method and if the player is hitting the ground then the y component after the move and slide should be zero or close to it anyway so then what we'll do is we'll set the falling speed equal to the y component of the result there we go and now we shouldn't have that craziness happen at least and we we fold out hey it kind of worked is that looked a little harsh uh left and right is still working wow um i don't know maybe you like that let's maybe i'll just reduce it down to five yeah i like that that's cool but we also want to die when we fall so let's add a new area to our game and we'll call it void let's add a new child node it's going to be an area i'm going to call it void we'll give it a collision shape and let's make this a uh we'll make it a box shape let's click on the box shape here and we'll make it let's make it 50 by 50 like that so it's gonna be nice and big and wide and then click back on the void here and we'll move the area we'll move it down and let's set it to oh this is gravity that's not what we want let's go to that transform down here and we'll just set it at let's say negative 50. so it's way down there if we look down we can go there we just i just want it i'm gonna hit seven to go into top view can i see it oh you can barely see it the blue outline there i just want it further out in case later on or for some reason players fall off the edge and then they like keep pressing over and trying to get past the edge of our void collision okay so we want to be able to detect when the player hits the void so let's go into the node over here and create a body entered signal and we're going to connect this to the player so on void body entered there we go and it's going to be similar to this here on stats you died signal we're going to do the same thing we're going to cue free the player uh but this time we'll type something it'll be game over be like [Music] okay uh let's see how that works there we go ah cool it worked okay um but this is kind of lame it if we play our game it's you're looking down into like this gray not very scary void why don't we make it black and also the player when they fall down they just kind of disappear at a certain point right they're just q freed as soon as they hit our little void collision box so let's edit the environment a little bit to make this look a little more fun um if you wanted to you could create a whole new environment by adding an environment node here a world environment you'd want that to be a child of your of your game and then in the inspector you could add a new environment click on that and you have a bunch of options here but i'm not going to do that i'm just going to delete that that's in case you were interested in editing your environment in a different way but i'm just going to use the default environment here if you don't have an environment node in your scene then it's just going to use this default environment in your project which you can see here has all those same settings so let's go into background here and we have a procedural background which is creating the sky and the ground and kind of this this different little horizon transition there i'm going to click on that and if i go to ground i'm going to change the bottom color to pitch black like that and because in our game we're basically just looking down you're not going to see the horizon that's going to work perfectly okay and so the next thing i want to do is add like a little fog which we can also do in the default environment what where'd you go default environment oh gotta double click it is okay let's not background here if we go into the fog section i'm going to enable fog and there's a few settings here depth begin and depth end that's how far how far away before we start seeing fog so if you set it really low you can see things are already starting to look foggy right and it has a default of 10. but because if you look at our camera here our camera's position is on the y-axis it's 18 away on the z-axis it's like 13 away from the origin so we're like you know i don't know somewhere around 20 or 25 distance and we don't want to have any fog in our game so let's make sure the fog doesn't start at least about 20 or 25 so let's go back double click the environment and then i'm going to type in i'm just going to type in 30 to be safe there and now we do want it to be completely fogged out we want the player to completely disappear or any object to completely disappear before they are cue freed in our void so the void is 50 down here and we're looking in our game we're looking at of our camera which is adding another you know 20 or so so we want to have the depth to end at around 50 plus 20. so around like 70 or something and maybe do it a little bit less i'll do 60 so that the fog fully takes over before the player hits let's take a look if we can see that pretty neat you notice how he turns like a weird gray color instead of black that's because this is the color of the fog and i want the fog to be back too i want him to actually just like disappear into the fog yeah that's cooler let's give one off the side here oh you couldn't even see it one more come on come on cool you might want to slow that down a little bit or you know adjust how far down it is and adjust your fog and stuff make it however you like this one's a little short so i'm gonna tweak our side to side movement here as well while we have a little extra time so let's go back up to our our movement here i'm gonna minimize that here so currently we are instantly moving left and right and up and down and there's no real kind of like a little bit of acceleration or deceleration or give so it's it's a little choppy so let's let's tweak that a little bit and to do that i'm going to separate uh our velocity which is how fast we're going with our movement direction which is going to kind of hold the data for which direction what keys we're pressing to move so i'm going to create a new vector 3 that's move direction i'm going to give it a tight hint here i might as well give the type in to our velocity as well and now instead of having all of these variables as velocity i'm going to change all these to move direction so this is kind of like our input direction movement direction okay so now every round our speed is going to add some acceleration each frame so we might as well add this here constant acceleration and we'll give it something like i don't know let's try try five as well see how that goes we can always adjust it okay let's let's let's see what that's gonna do we're probably gonna like we're oh my gosh we're moving so are we moving we're not even moving why aren't we moving because we don't even use move direction of course so this velocity here uh should be move direction normalized move direction times the speed definitely accelerating definitely and i probably fell off the edge okay um so we clearly need a max speed so under our speed we'll create a new variable called max speed and we can set that to something eight maybe just like we had speed before but our current speed is going to be zero right we're going to accelerate from speed 0 up to our max speed and we're going to be doing this at this many speeds per frame 5 that's probably way too high right so maybe this should be 0.5 or let's try 0.1 so it's going to add 0.1 to our speed every time we're not actually using max speed yet but let's just see if we can get an acceleration that looks better okay cool um and then if we change if that was too slow we could go 0.5 and then definitely and you can see we never reach the max speed it just keeps going up and up and up right so what we want to do after we accelerate each frame is we're going to set the speed to be the lesser the min of speed or max speed so if the speed is lower we'll use it but once we've once speed becomes more than max speed we're just going to set it to the max speed so it'll never go above that and then this is where you'll be able to start playing around between those two numbers between the acceleration and the max speed and if i set the max speed a little higher let's set it to like 20. just you can really feel it that oh yeah okay definitely a difference there so i'm gonna set it back down to eight and we only currently so it accelerates nicely kind of does it well here i'm going to make the acceleration really small and just to show you something here 0.1 small and we'll set the max speed to 20. so i get up to my max speed but now i'm at my max speed it never resets right when i stop it doesn't go back to that initial zero speed i'm just instantly at my max speed again so that kind of defeats the purpose so i'm going to leave these values extreme just so we can feel them and then we can tweak them later so we need a way to see if we're hitting the arrow keys and moving or not moving so that's pretty easy because we can go if not move direction remember move direction each frame is going to be reset to an empty vector and if we're not pressing any buttons it's going to still be an empty vector and that's going to be a falsy value here so if we're not moving then we can set the speed to zero else we can do all this other fancy stuff and we'll leave that there so now at least we should accelerate slowly and then we stop and then we accelerate so every time we stop we accelerate right okay so that part's working however i don't want to instantly stop when i stop pressing i actually want to decelerate as well and i can do the same thing here i could do subtract the acceleration and then just make sure i don't go below zero but there's something that's a little nicer here and we don't actually need the speed at all what we'll do is we'll set our velocity which remember at this point does not include the vertical axis the y-axis for gravity we're keeping that logic separate so what we can do is that we can set the velocity to itself and with an interpolation to zero using the move toward method so we're going to move towards an empty vector 3 we're going to go from our current velocity to no velocity and here we can give it a delta or a float or a percent change value and this is where acceleration would make sense so let's check this out so i'm speeding up oh i'm getting faster the speed never resets to zero of course um so let's pull this velocity since we're setting the velocity if we're not moving the velocity is going to slow down towards zero and if we are moving the velocity is going to speed up up to our max speed there we go so we basically have a nice level here right now we're still not resetting speed to zero so that's a problem um so although this logic visually seems easier to read what's going on um maybe let's just simplify the movement logic to use this move toward method as well so i'm going to ctrl k this and what we can do is we can see that our target velocity is the movement direction normalized times the max speed and what we want the velocity to do is move towards that so let's go velocity velocity oh my and then we'll use the move toward method so we're going to go from whatever our current velocity is it might be zero might be something else and we're going to move it towards the maximum velocity in the direction we want to go and we're going to move by our acceleration value and if that works hopefully i didn't delete all that stuff prematurely let's check that out that's it yeah now we get now you get um the real sliding on the ice where if you're moving in a direction you try and turn you don't instantly change directions you actually have a little bit of momentum going on here okay oh yeah the momentum is doing a little bit of crazy things here i think you can build it up against a wall here watch this cool okay so obviously we don't want to max b20 let's set a max speed of eight and let's not let's be a little quicker on our feet here maybe we can set this to 0.5 for our acceleration and get something that feels a little better oh that's still a little laggy it's kind of fun though actually you might want to play around this see what you get i i think maybe i want acceleration of one max speed is probably still good oh it feels way more smooth than we had before um and uh now we have an easy way to make future ice levels or power ups with increasing our acceleration i'm dead
Info
Channel: Age of Asparagus
Views: 339
Rating: undefined out of 5
Keywords: AgeOfAsparagus
Id: 0GzYiOUpoIA
Channel Id: undefined
Length: 18min 41sec (1121 seconds)
Published: Sun Jun 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.