Godot Vehicle Tutorial (VehicleBody): (Armored) Car

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to my first ever video tutorial today i'll be teaching you my setup how i make things like cars tanks using the vehicle node um so i have a blank project right here the only thing that i've added is my vehicles folder which contains a vehicle i made in blender along with its materials but i'll be showing you that later on so first things first let's make a test area to work with so click on the 3d scene up here rename the spatial node to worlds add a static body as the child which is going to be our floor so after that create a collision shape for it it's going to be a box shape x by 50 and z by 50. after that you can see we have a big area to work with we're just going to add a mesh to that so we can see exactly what we're working with like that and then finally change the size to 100 by 100 and add a new material by clicking on the materials tab and new spatial material click on this albedo and click and drag the icon.png so as you can see it's a bit ugly um but you know it is what it is click on the uv one click on track planar and it's going to pile it along the mesh so we can see things like when the car is moving about how fast we're going direction we're going um yeah just stuff like that so that's pretty much done we're just gonna save this as our world.tscn there we go and now we're ready to start working with our vehicle so if i open up the model that i made in blender new inherited uh as you can see it looks a bit complicated but you know it's not actually that complicated um we have a few meshes to work with we have our body we have our torrent which is this thing right here excluding the gun and then we have our gun which is this thing here and then we have our four wheels front left back right back left front right yeah so that's pretty much it um what we're going to be doing is we're just going to be changing our spatial node into a vehicle body node just like that and then it says here that we need to have a collision shape so click on the body click on mesh create single convex collision sibling drag this up make the body a child and then rename this to body just like that so we're going to be doing the same thing to the torrent as well mesh single convex collision and yeah drag it in like that rename this to turren and then finally what we're going to be doing is we're going to be dragging the gun here because what that's going to do basically is once this starts rotating it's going to rotate along with the turret as well which is exactly what we want so this is the part that's a bit tedious but it is what it is um as you can see we have our four wheels here all we're going to be doing is we're going to be creating the wheel as a child because what we want is we want the vehicle wheel to be created in the origin point of the mesh itself but we want it to be the parent so this is the tedious part we're going to drag this out drag it back in and then rename this to the appropriate name like that so i'm doing the same thing for everything else as well add child mode vehicle wheel drag it out drag it in rename it vr same thing with this as well add child node vehicle wheel drag it out drag it in real bl and then finally the same thing for this like that drag it out drag it in rename it wheel fr front right so that's pretty much done in terms of setting up the vehicle itself um now we'll be going over the properties of each thing on the wheel node itself so we're just going to go to our front left wheel there are a bunch of things that you can look at you can actually hover over it so it kind of briefly explains what each thing does but for simplicity's sake we're just gonna be concerned with these three tabs right here so vehicle body motion because it's the front wheel it's gonna be our steering wheel so we just check steering like that wheel and then afterwards as you can see here we have our radius rest length and friction slip so the radius is perfect right now basically it kind of goes outside the wheel which is exactly what we want but if i change the value for like one as you can see it expands like this for things like big wheels small wheels um just kind of scale it appropriately to whatever size of wheel mesh that you got so 0.5 works for me um great rest length is basically if you can see this spring right here it's the distance of which the wheel travels from its original position so as you can see if i change it to one the spring elongates so this is going to be moving a longer distance from its original point which isn't what i want um i actually want it to be exactly where it is so 0.15 works for me and finally friction slip so friction slip is basically if you want your cars to drift um or if you want to simulate things like wear and tear of the tires um as you can see in the description it says zero means no grip one is normal grip so i'm not quite sure why it starts at 10.5 but let's just change it to one just for the sake of it being normal grip and finally suspension travel is basically um the distance of which is going to travel up and down from its center point i don't want it to travel too far so i change it to 0.1 and finally stiffness is like the bounciness of the suspension itself i like setting mine to 50 um as you can see if you read the description um 50 100 200 depending on the type of card is but for simplicity's sake i'll just put it as 50 for the time being so that's that for the front left wheel for the back right wheel um it's gonna be the same process as well the only difference is um we're gonna be changing the vehicle body motion to attraction because basically it's gonna be the thing that's gonna be pushing the car forward wheel perfect everything's good the only difference that i'm going to put is the friction slip i'm going to be changing it to 0.7 because like i said before i want the back wheels to be slightly lower than the front wheels so i can start drifting my car a little bit and suspension changes to 0.1 and 50 and then do the same thing for the other wheels as well so i go here because this is a back wheel traction yep yep friction slip 0.7 [Music] suspension 0.1 50 and finally front right wheel it's going to be a steering wheel friction slip one suspension 0.1 50 just like that so that's it pretty much in terms of setting up your wheels the only thing we need to do right now is we're going to be sending a camera for the um car to work with but there's one thing i want to mention as well if you click on the original uh vehicle body node you'll notice that the origin point is slightly well it's in the center right but this isn't exactly what we want the lower the origin point is the less likely it is to flip over um so what we want to do is just click on everything here click on the snap and then bring it up by one so that way when we click on the vehicle body node you'll see that it's just on the ground like this which is exactly what we want yeah so save this save it as humber because that's what it is and we're just going to be adding a camera because this is the thing we're going to be working with right so let's just add a camera the way that i set up my camera at least is i make a spring arm first and i'll just drag it up so you can see what it looks like what a spring arm does it if you see that line basically whatever is attached here will be kind of moving along that line and whatever collision occurs it's going to be moving along that line accordingly so if i attach a camera onto it which i'm going to do anyway clip camera just like that if i click on this thing right here to test the scene as you can see the camera moved back along the spring arm as opposed to being just here so what i don't want it doing is i don't want it coming in contact with the vehicle itself so what i tend to do is i tend to change the spring arm collision mask to being on layer two and then i'll be adding a code later on to the spring arm just to show you how to avoid this entirely so save this go back to your world and import your tscn make sure that it's not on the floor take off snap up and then set your main scene as your world.tscm yeah so as you can see uh the vehicle is now in the world everything is working fine beautiful so now i'm going to start adding some code onto the vehicle itself but before i forget we're actually just going to change one thing on the vehicle body so click on the vehicle body and as you can see here we have the mass and the weight so usually for most cars they're 1600 kilograms because the weight is measured in kilograms we're just going to change that to 3 200 just because it's an armored car and it's going to be heavy right so yeah that's pretty much it so what we're going to do now is we're going to go to project project settings and we're just going to set a few uh key bindings so w s d followed by a space and then esd for escape so if we add it by clicking on the plus sign just like that now we can start adding the code onto the vehicle body itself so right click and go on to attach script hummer.gd and let's set a few variables so the variables are going to be setting is horsepower which i'm going to be setting to 200. equals 200 var acceleration speed is going to equal 20. var steer angle so what this is going to be is basically going to be the max angle of which our wheel can rotate we're going to be using a function called deck to rag which is basically degrees to radians so usually when it comes to rotations is calculated in radians so to make things simpler we use this function to convert a degree into a radian and after that the stair speed is equal to 3 and then bar brake power is equal to 40 bar brake speed is equal to 40. so basically what's gonna happen is once we click on the brake button it's just gonna alert straight to that as fast as possible and we're just going to add the function physics process so usually with rigid bodies you'd be using integrate forces but for some reason i found a lot better success using physics process instead at least for this particular instance so what we're going to be doing is setting a few input variables so var throttle input or throt input is equal to negative input dot get action strength w plus input dot get action strength s and then engine force so what the engine force is if we go back to the vehicle body node it basically determines if you're going forward or back so negative values meaning um along the negative axis which is basically forward and then positive values meaning you're going backwards so if we go back to the script we're just going to be lurping linear interpolating our engine force to our throt input times our horsepower so what this means um if you're pressing w so it's going to be a negative value it's going to be negative 1 times horsepower and it's just going to be lerping to a negative value negative 200 and moving you forward at the speed of excel speed times delta and yeah the second one is going to be our steering for our steer input oops yeah bar steer input is equal to negative dot input get action strength this one's a bit different though because when it comes to steering um it's kind of inverted so d is going to be our left you'll see when i stop playing the uh project itself dot input get action strength a sweet and then steering is equal to lerp angle so basically linear interpolating the angle steering steer input times your steer angle your max steer angle and then steer speed times delta and finally our last variable that we're going to be inputting is our brake input which is just going to be input.get action strength space because that is our break for now and then break is equal to lerp brake brake input times brake power brake speed times delta so let's just put some comments so we know what each thing is this is going to be our throttle this is going to be our steering and this is going to be our braking in terms of coding um that's pretty much all you really need to move your vehicle around if you want to change how fast the maximum speed can go if you want to change how fast it gets there if for some reason you want to be able to turn your wheels and even greater or less value you can adjust this and how fast it turns and the brakes etc so let's just run the project so you can see what it looks like right now so as you can see my project if i press w which is forward it moves forward if i press a it turns left if i press d it turns right if i press space it breaks and then if i press s it reverses just like that so what we want to do now is just be able to move the camera so i'm going to be adding some coding to that so you can see what it looks like from the side front and just get a better view of what's going on so let's go to our spring arm over here what i'm going to do is i'm going to rename it to camera instead and i'm going to attach a script camera.gd yeah and function ready so the two lines of code that we're going to add input dot set mouse mode to input.max mode captured as well as clip camera dot add exception get parent so like i said before what i don't want my camera doing is i don't want it uh getting stuck behind the model itself so this lineup code fixes that as well as making our mouse invisible up here so that way it won't get in the way of well immersion i guess function input event so we want to be able to toggle between these two modes as well so let's just line add a few lines of code for that toggle mouse mode if input dot is action just press esc if input dot get mouse mode is equal to input mouse mode captured input dot set mouse mode input mouse mode visible else input dot set mouse mode i also want to capture it what this means if i press the escape key it checks if i'm currently in the mouse mode captured if i am set my mouse to visible otherwise set it to captured done next one move camera rotation actually so the very first thing you want to do is you want to check if the last mode is currently captured so if get mouse mode is equal to input.mouse mode captured then if event is input event mouse motion rotation degrees dot x equals clamp rotation degrees of x minus event dot relative dot y times 0.1 negative 45 45 notation degrees dot y minus equals event dot relative dot x times 0.1 so this line of code here basically it clamps the up and down rotation of the camera to 45 degrees that way we don't flip our camera entirely and this one is just basically turn the camera around so let's look at the project right now and see how it looks like so as you can see now if we move around the camera everything works as intended up and down is locked at a 45 degree angle the camera doesn't get stuck behind the vehicle but it still collides with the floor which is exactly what we want if we press the escape key we can toggle between mouse modes so for example you can have menus and stuff like that if you want to click on without it affecting the camera press escape again go back to moving the camera and then controls still work as usual so you can move around yeah brake reverse and that's pretty much it yeah so i'll be adding some other stuff as well how to move the turret how to shoot the gun stuff like drifting adding particle effects if you guys want me to look into anything in particular i'll try my best to research and make it a tutorial for the next video otherwise yeah um second part will be in a part two and i'll get it done if you enjoyed what you watched feel free to subscribe and have a great day
Info
Channel: Jus'ko
Views: 7,875
Rating: undefined out of 5
Keywords:
Id: Hn74y_wdcF4
Channel Id: undefined
Length: 20min 12sec (1212 seconds)
Published: Sat Dec 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.