Low Poly Racing - Making Of - Episode 4 - Unity and Blender

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay [Music] alright guys and welcome back to another video this is episode number 4 of the low-poly racer and I know you've been waiting quite some time for this one and so a lie first of all my wife had a birthday so that was really nice we spent that together and I've done the Ludum there in the mean time as well that was a lot of fun they wouldn't dare 46 check out my game a little Elliott alien rescue and I also did the rigify video so if you haven't seen that one they should have a look at that one and I've done the usual ten minute modeling videos as well as working of course it's got a full time job doing an RTS game now it's good a lot of fun one more thing I've changed it up a little bit because when I made the low-poly racer I did a lot of things back and forth I changed them around and then I changed it again and again while I was testing to develop I usually do that when I prototype a lot and instead of having to drag you through that entire process of doing something and unchanging it back and then doing something and then changing again probably gonna be a lot of confusing so what I did was I tidied up the project a little bit and I restructured some of the code and the hierarchy of things and then I've scrapped that put that to the side and I've got a project now similar to where we left off at last time so I've only got the basic track and the little police car but I have to read I'll take you through writing the scripts again and hopefully should be a little bit of a better code I have to warn you though I'm not really the greatest of programmer so you're gonna find a lot of stuff that you can make more efficient and more straightforward and definitely more object-oriented this is to my ability I'll do something that should be hopefully easy enough to follow along and also easy enough to do and again performance is not gonna be an issue for a game like this so I'm not too worried about that but the reusability of code and things like that could be improved but let's see where we left off pretty much I've got the car here as you can see if I press play now nothing will really happen because I even scrapped the the input script so we'll redo a little bit of that and we'll also redo a bit of the wheel Collider stuff and if you remember it if I expand this car here now where we left off we had a bunch of transforms and also the wheel colliders so instead of doing that I thought I'd simplify it a little bit so let's move these shift select these and just drag them into the roots here of the wheels then we can delete this wheel Collider one altogether and then what I'll do as well is just drag these these are the actual wheels here the transforms so I'll drag those instead as children of the wheel tractor will collide your objects here and then will delete that one as well so all we have now if we do this is let's collapse and expand we just have wheels and then we've got the four wheels here and then this is just got the wheel Collider for now and nothing else and under here we have the measure as well we can just rename this one f2 and put mesh here let's do that for all of them actually if I hold Alt key it'll do for all of these souls just mesh so maybe that's a little bit better structure we have the copy car here and we've just got the wheels here and then we've got the colliders that we set up before and we've also got the center-of-mass transform here someone suggested as well which is actually quite handy and that's on this Machir it's scale one here but on the other mesh here the right front one trick to do is to just do negative one here on the X scale and that'll keep the code down the same we ought to do some tricks before when we did the set the position so there's a little trick thanks for those who use suggested that so both the right wheels here the meshes are flipped on the x axis here as a negative one as you can see again like the car isn't moving because if we go into the script I took away some of the input code and some of the wheel alignment code and let's change this around a little bit to make it more reusable anyway the first thing I'm gonna do among I'm gonna create a new script here and I'm gonna call it wheels or just wheel and in this one instead we'll set some properties here for it so let's do a public boom steer that's just an on-off on a flag if we want to steer the car or steer this wheel in particular we'll do inverse steer as well because sometimes if you have rear wheels on a car that you want to steer like a forklift truck for example you might want to invert the steering so it turns the other way and we also want to set the power that could be applied to the wheel so if it's a real drive for example or a four-wheel car drive so also it's not really best practice when you do programming to expose valuables public you can actually do them private and then expose them to inspector but then unity will warn you that they're not assigned a value so I'm just gonna keep it really simple now I'm aware that you shouldn't really expose public variables like this but it's the simplest way if you want to get the job done and it's not really that much of a problem okay what else would we need we need to set the steering go so we'll do a public float here and pull out steer handle and then we'll make this a property so I guess sex property and this way it won't be exposed in the inspector will also do at work here yet so this is just a public property that we can asset so let's add a couple of private variable here as well we'll do a private wheel collider pull up wheel collider do another one called transform and this is for the wheel transform and instead of assigning these in this Specter let's make the script automatically grab these so we can just assign them here in the start Mona behavior that'll start up every time unity starts up this component this will run automatically so we'll do wheel Collider that's this private variable that we assigned here and then we'll do depth components in children wheel Collider and now when this script starts up it'll look in the transforms child hierarchy and see if it finds a wheel Collider and if it does it'll assign that to this wheel Collider variables so we don't have to assign that in the inspector then this will just pick it up automatically so if we adding a lot of wheels you'll say what's the hassle of having to drag that and the other one will do as well wheel transform equals get component in children and here we have to grab the mesh renderer instead and then we'll get the components transform and the reason why I don't get the transform to begin with is because the first child will actually it it'll have a transformer well so I can't just grab that one you'd grab the same one as the wheel collider is so that's why I'm looking for a mesh renderer in this case because I know that one child will have the wheel rendering and that's the one I want to grab the reference to for the transform then what we'll do is just swap back here and then to these wheels now we'll just drag this wheel component and that automatically assigns it to all of them here and then we can set that the two front wheels here they're gonna be steering wheels and the rear wheels here they're gonna be powered wheels so the reason why these pop up here is because we expose those here in the inspector but we didn't really get the steer angle and the torque and I'll set that from elsewhere that's why there we set those if you remember it that we set those as properties here they get set here if you don't know what this properties are maybe do a quick Google search on properties in c-sharp so with that said we also need to in the update if you remember the last script that we made then we had to get the wheel Collider to set the rotation and everything of the transform okay let's just press play and see what happens now not nothing will really happen at all apart from what still doesn't listen to any input like what we had before and the wheel colliders are just sitting here not doing much of a job at the moment all we've done is just add a script to prepare for things we should actually start doing the input now as well okay we're still just catching up to where we were before really but let's add a little bit more code in this wheel one now the update method here runs every frame in unity and if you remember last time we put some code where we had to get the wheel Collider to send its position and rotation to the transform so that's what we have to do for this new wheel components as well let's do in this update let's do wheel collider get world posts and then we'll out that into a vector three which is called posts and then out the rotation which is a quaternion to rot and then here let's do wheel transform we need to set now position we'll set it to this variable and the wheel transform rotation our and then you can just press the tab in Visual Studio to get it to autocomplete the most likely one so equals rot and then we also need to do some stuff in fixed update too so update runs every frame every and if you have a normally it run at 60 frames per second if that's what your monitor is set to but you can also people might disable the vsync and they could run at even a thousand frames per second so this update loop runs really often and there's another one that is called by unity every fixed update it's called and the physics engine runs at a hundred Hertz by default so 100 times per second so this fixed update will be called a hundred times per second and now we'll do if steer so we'll check if this is a steering wheel not a steering wheel but a wheel that's stairs in the car so if that one's set to true then we'll do this wheel Collider steer angle equals steer angle which is the this property that we were gonna set from elsewhere and then we'll here we'll do a check if if we've enabled invert steer invert steer and here we'll do a shortcut for an if statement so it'll check if Imbert's there is set then it should do minus one and if it's not set then it should set it to one here and that's just basically to flip the steering angle if this bull is set or not and then like we did with steering let's just do where the power as well will do if power so if this wheel is about to apply power and we'll do wheel collider motor torque equals torque and that's it that's our complete the wheel script now and it's still not gonna do anything when we press play because it's basically it just sits here and it's waiting for input now so nothing is really happening and for that to happen we need to start making an input controller so we can start listening for the keys and I don't really want the car itself to listen for keys like I did before in the previous example because I want to be able to reuse the car so when AI could be able to drive the car for example so instead we'll first of all we need the game manager so this just creates an object here called game manager yeah that's right and then we need one more script here and we're gonna call this input manager or we call it input controller actually okay and then we'll double-click on this input controller and here we'll do some more universal code now for that the game can listens to your input instead of the car doing it will make this input controller listen to all the input so we'll do first of all we'll do a public string here that will call input steer axis equals horizontal and this is the default if you look here in unity we can go to the edit project settings input you can see that there is a bunch of predefined variables here and one of them for example is the horizontal axis and that's the aid of a and D keys it'll listen to and also we can listen to a joystick x-axis and there's another axis here that we're going to be using which is the vertical one so you can change those around here but I'm just gonna use these default ones so we'll do the same here we'll do a public string input throttle access equals vertical so when whenever you do up and down or W and s on the keyboard it will ruffle the car forward okay and then we'll do a public float here and it's going to be a throttle input yet and another script could get this input now and because it's exposed as a public property but will only do a private set here so only this script can set it only the input manager itself has set this variable or is property and the same thing will do steer input and this is also I get that anyone can get this information but it's gonna be a private set only this script can set it don't need anything in the start one here in this update loop now all we'll do is steer inputs equals inputs get access inputs steer axis and then we'll do throttle input equals input stop yeah X inputs throttle axis so let's recap this real quick the only reason why I've set these to public it's because you can customize those in the inspector then if you want to be able to add some more axis or a change it to like an hand controller for example so you don't have to do that you could keep it private and hidden if you want but then in this update loop all it'll do is it'll listen to the input.getaxis here and take the steer axis and the throttle axis and put those values into this public property so other scripts can get it so we'll store this throttle input and all and also this steer input so now we know that any inputs that's gonna control any car is gonna be in this input controller script ok nearly there now let's also do we created this game manager script and that's empty at the moment like this but let's create a new object in our scene here now and call it create an empty here and set it to 0 so it's in the center of the scene and then f2 and rename this one to game manager and then we'll drag this game manager script onto this one and this is an empty script I won't do anything yet but under here I'm also gonna create a new empty and I'm gonna call this and kind of gonna cut I'm gonna call this input controller and then we can drag the input controller script to this one here here you can see the public variables that we set the horizontal and the vertical axis here so they're exposed you can change those two axis one two or three or something like that if you needed to change it to a different controller okay then we go back into this car script we need to do a little bit of tweaking here as well so we'll define a new variable here and let's call this one this is gonna be an array a wheel array of the wheel component that we created that wheel dot CSS script just call this wheels and then here we'll just grab a reference to this now so wheels equals get components in children wheel like that so since the let's recap this one as well so on this car script here let's see what it does it's sitting on the car here and we've got this car script here and under here we have a bunch of wheels but they're further down into the unity hierarchy here but this one's got a wheel script and so does this wheel and this one in this one and if we look at this script again what does it do in the start Mona behavior here it'll look for all the components in its children and find this wheel component that we're looking for and then it'll return all of those and put those into this array of wheels and this way we could have a car with an 18-wheeler truck for example it'll grab all of those wheels and put them into this array here and now in the update all we need to do instead of typing the code over and over for each wheel let's just do for each so we will do a for each loop for all of the wheels here bar wheel in wheels and then we'll do wheel dot steer angle equals sphere multiplied by the Maxis steer and we'll do wheel torque equals ruffle times motor so again if we had one wheel it'll just do it for one wheel now we've got four wheels and they'll send the steer angle and the torque to all of those four wheels automatically and this grabs the components and puts them in the array and in the update here ill for each all of these wheels and then apply the steer angle and the torque and remember that we've configured that wheels can listen or not listen to this information as well because if it's got on this wheel here it'll only apply the steer angle information that it gets if steer is enabled here so if steer that's where you're gonna the steering information is actually sent to all the wheels but it's only if this bull variable is set to true that it will actually apply the steering okay we're getting closer now so we're still not sending the input so it was still and just bear with me there's a bit of a restructure to the code it'll make more sense later on probably but if we press play still nothing is really happening but we're getting close now we got the basic input controller now that will listen for the input and that's happening right now we've got wheel slides ready to receive information for the steering and the torque now we just have to connect those two together and make the inputs have been sent to the wheels and now we're gonna use this game manager to not gey manager game manager we this is still an empty script and the only reason why this gets a cogwheel icon you don't have to worry about that I think it's just unity thinking that this is such a common name for a script that'll change them in the icon for it but it's just still just a c-sharp script like any other year so it's the same let's go into that one and here we're gonna just make sure that other objects can call this one now and we're gonna make it into something called a singleton and if you google singleton whole internet will scream at you and say don't use it but all the alternatives are a lot more complicated to get into all the other design patterns so I think if you're not sure of how to do other stuff this is a good way to start it because make a simple game learn the simple parts first and then learn more object-oriented languages and better design patterns later on in our our chess game we're using dependency injection instead which is a different pattern that you could use and but just to keep to the simple stuff let's make this one a singleton and very basic one as such so let's just if you're gonna do some hate in the comments feel free to do so we're gonna use a singleton anyway and we're gonna do this quite simple so everyone can follow all right so we're gonna make it possible for other scripts too regardless they're gonna be able to find this script without having a reference to it so we'll do a public static variable and it's gonna be a game manager variable so same as this object and then we're just gonna call this instance and it's gonna be a property so others can get it but only this one can set it and we'll do here we'll just do in the start here we're gonna or actually we'll change this to awake and here's another concept I'll go through this in a I'm gonna do a video where I go through one of these concepts more in detail about the different Mona behaviors like a wake and start but for this purpose a wake runs before start so it'll run awake on all the scripts first once it's done with that I'll run start on all the scripts and then it's just basically I'd like a multiple staged rocket where you can put types of scripts in the different stages and we want to make sure that this happens as soon as possible so it's gonna happen when this object is awoken and here it will just do instance equals this and that's the most simple type of singleton that you can do and others can find this script now by just referencing this static variable but you have to be careful because if you added this game manager to multiple objects it won't work you can only have one game manager and we're not really protecting ourselves from that in the code but just to keep it simple now I just want to keep it like this for a lot of you this is probably weird enough just as it is so I'll just keep it very simple at this point the input needs to reach from the input controller that we've created to the cars wheels ultimately that's what will need to happen so here we'll do a public inputs controller reference or a property we'll call this one input controller others can get it but only this one can set it and then what we'll do is here in the awake I'm on a behavior then we'll do input controller equals get component and children inputs controller so this one will as soon as it starts now it's going to look in its children and it's gonna see I want to get a components called input controller and I want to store that in my property here that others can use so when the script runs in the startup or in the awake here it'll look down in its hierarchy and they'll find oh I've got an input controller here and it's got this script to it I'll create a reference to this one so finally now we cannot use this in in other scripts so if we go back into the car script now I'm actually going to move this code elsewhere again but just to keep it really simple in the beginning in this update loop here now we'll just do steer equals game manager dot instance and here's where that singleton comes into play because we've referenced the static variable or we've got a static variable name now called instance and that one will return the only instance of the game-manager that we've got and this one in turn has got the input controller that we configured and then we've got the steer input so if we look here this grip will go oh game manager that's a component that we've created or a script and that's got the static property called instance here so if you go back in the game manager and we've got a static instance manager here we've got a static game manager instance here so back in the car script here okay will we found the game manager we found the static property instance and now we want to find the input controller and that's set here in the awake first of all we set the instance to this which is this component and then we've added this input controller now it's found the input controller in its children so now the car can find that one in the scene when it starts up and it can actually find this all the time now so in this tier it'll just get the steering puts and store that into its own steer property here and then we'll do throttle equals game manager instance input controller dot input and the reason why I made these properties of the car script is because I'm gonna break this out into a player script later on because you don't necessarily want the input manager to send the information straight to the car what if an AI is driving this car then you want to have a different components ended up but just to test this finally out now a lot of talking here there's probably gonna be the most disliked video of all because now I have to spend a lot of time writing script where nothing's happening but now when I press forward the input manager in the scene will listen to the input and it'll apply it out to the car and it'll apply it all to the wheels so that was a lot of code now so I should probably recap this one because there's a lot of stuff to take in so if you want to fast forward I'll put the in the description I'll put some timestamps a little bit but now let's recap what did we actually do here so first from episode 3 what we did is we moved into a simpler hierarchy here so the wheels with the wheel Collider is just here in the you know you know wheels subfolder or sub game object and we got the left front right front left rear and right rear that's just got the wheel Collider on it and it's got the wheel script that we were created okay and under this one we've got a mesh one and this has got the mesh filter with a wheel come the wheel air mesh on it it's got a mesh renderer that renders the wheel and then here we've got so that looks the same for all of these wheels but what we did differently now and a little bit better than we did before is that we actually added this component the wheel script and let's bring that one up so the wheel script has got its got three public variables here and we can configure if a car should be able to steer with this wheel if the steering should be inverted or and if it should be powered this wheel if we look back here now we can have the different check marks what if I just unchecked the steering now for these two wheels can you guess what will happen you guessed it can't steer it I'm pressing the keys now nothing is happening on the real wheels now let's just enable steering here so what happens then it's sort of like a forklift truck now but not because now I press left on the key but the car goes right so if this was a forklift you drive yourself mental because it go the wrong way the whole time so we can invert this steering here on both of these I've got both selected and now when I steer it it'll be like a forklift truck where you still with the rear wheels instead okay but let's bring back so we've got just power on the rear wheels and on the front wheel sir we have steering but what if we add power to the front wheels as well we'll have a four-wheel drive car it'll accelerate a lot faster and be a lot more rapid so make that by breaking this out into a separate script now it made it a little bit easier for us to customize the wheels and we could have six wheels eight wheels 20 wheels 18 wheels whatever you want and you can just configure them as you need instead okay so that was one of the things that we did okay we also simplified the wheel a little bit by the wheels themselves we'll look at the wheel colliders configuration the position and rotation of the wheel and they'll set that to itself or its own transform it gets the component here by looking for a mesh renderer in that way it finds the actual visible wheel on the car and it gets the transform component on that one and then in its own update loop here every frame it'll look for the we are colliders rotation and position and then it'll apply that to the transform here and that will do it no matter how many wheels we've got we need don't need to repeat this code anyway and then in the fixed update here if it's steering is enabled then it'll send the steer angle that it's received from somewhere and they'll send that to the wheel collider and same thing for the motor torque it'll see if it's gotten any torque sent into it from anywhere and if it's got that then it'll apply it to the wheel Collider and it's the car that sends this information at the moment we can actually get somewhere else can set this steering angle and the throttle but at this point it's just descriptive in itself here in the update but it receives it from the game-manager instance input controllers steer input and then it'll get the steering and the throttle here from the input controller and then you'll look loop through all the wheels that it knows about and it'll send the steer angle and the torque to those wheels and here's the reference we've got a private array here of the wheel component or wheel class and then in the start here it'll look for all the components in its children and it'll find those wheels and assign those into here that's why it works to loop here and then the car can send this steer angle in the torque and let's recap as well we did a simple input controller that listens to input from the horizontal and the vertical axis and it just stores that into this these two properties that someone else can look for so if we had ten cars actually let's do that now this is where this comes in a little bit handy if we made it a copy of this car now just control D on this one send it to the side here a little bit and without changing any code now both of these cars will actually receive the inputs automatically so they'll steer together and they'll roll over together okay the car drives out of view now so let's create a simple camera script now as well that so we can follow along with the cars so let's right-click here on script in the scripts folder do create C sharp script call this one camera followed and here it will just set polic tons for targets or engines now we'll do public vector3 offset Victor the Euler rotation thanks for correcting me I have always said Euler rotation but it's called Euler rotation and then we'll do a dumper as well so public float dumper and then here in the start will do transform Euler angles equals Euler rotation and here in the update let's do if target is nope then we don't want to do anything so let's just return I don't want to continue in this loop but if the target is set that will do transform position equals vector three under a linear interpolation from its current position air so it's from transform position to targets position plus the offset that we've configured and then we'll do it dumper times time time and this way it'll smoothly follow the car to whatever we've configured it to so we can do here on the camera now on the main camera let's assign this component will just drag it onto the main camera here camera follow and then we'll drag this copy car here to the target and then we need to configure this as well so we got some values and to figure out that then you can look pretty much about the cameras current position and rotation is if we move this car actually back to center scene so we know it'll be easier to do the references here but we don't really see the car now because it's under this here it's under there that's okay we want to see where do we want the camera roughly so if we set it to here maybe 21 thirty-eight maybe something like this will be good the cars there pretty much Center so now we know pretty much what we're gonna set these values to 38 21 62 for the Euler rotation and 180 on the y axis and the damper is set to one for now and when we press play now we should have the camera start following the car quite smoothly that seems to work pretty good so that's all we need to do for now for getting the camera movement to work quite smooth and nice okay I should also show you that I've played around a little bit with the suspension and forward friction and the physics I need to mention that I don't think I mentioned that yet and first of all to get this into a more cartoony car game then you really want you don't really want the real-world physics it'll look let's actually switch that back because I've changed that without your knowledge so shame on me if I go to the project settings here and go to physics you can see that I've actually ramped up the physics let's set this back to minus 9.81 which is the default value in unity we can actually drag this one into here because we're going to access it again and then I press play here and now you can see that it'll behave really weird first of all it'll roll over there because I've got a lot of friction at the moment but let's say try to make it to this jump here so if I launch myself on this jump now this is why we don't really want to real-world physics in it because the speed is so much greater than you would have in real life that it doesn't really work for a cartoony type of game or a small-scale game so I've actually bumped up the friction here to minus 30 and that way we can have it it'll make it possible to clear the jumps but so that's why we've bumped up the physics here and also I don't know if I showed you this so I have to show other added another layer here called wheel collider and if we look here we've got the wheels have set the layer two wheel collider and the car itself is just an another layer called car and the reason why I've done this is because you don't want the wheel colliders of the car to collide with the car itself or with those colliders so in the project settings here we go to physics we can see this collision matrix the wheel Collider does not collide with the car so also I've disabled it for props that I'll be adding to the scene as well so the wheel Collider does not collide with the car that's why I've separated those into different colliders and then also you should look at these settings now for the inspector if you want to copy these because on the front wheels I've got these are the trickiest things I think with the wheel colliders is to set their correct or the proper settings that you feels most right for your game I currently explain exactly what these do because I'm not so good at in themselves but it basically controls what's the well value for when the friction of the or the wheel should let go like let's say if you do in a corner and then it starts to lose their grip but it needs to pass a certain threshold before let's go but once it lets go then it'll basically require less friction to keep it sliding I won't be able to go into details but for the cars to behave like I have if we bumped up the physics now to minus 32 for the for the gravity then these are the wheels values that we got for the front wheels I've got quite high values here compared to the default because the friction is so the car has got a lot more power than you'd normally have because we want this car to any fast acceleration and with the with the heavy with a big gravity as well you'll pull the car down really hard to the ground so that way we need to bump up the these values quite a bit to get it to behave so it actually gets grips and sensors so it doesn't just do wheel spins basically and for the rear wheels other these are the values that I've got set I'll freeze the picture a little bit so you can have a look at those but you can just copy these values for now and I suggest that you start playing around with them because you might not be happy with the way it feels the way set them so you can play around with these values quite a bit to see what fits your game best you know the car is a little bit front heavy so let's this Collider here I think he's messing up a little bit and let's move this Collider if we do a jump it looked like the car was a little bit front heavy so if we do a jump here you can see the car tumbles forward like this and that's because this Collider let's make sure that one's in the right place here so we'll go to the colliders here we can see that this one is a little bit off so let's go to this Collider here and they'll be this one yet and then slide it forward a little bit on the z-axis there I'll have to make it taller as well so on the y-axis here again this is not so precise because the collision will be fine it doesn't need to follow this mesh perfectly maybe something like that let's try this okay still tumbles forward and in that case we can move the center of gravity as well here so this we've got this center of mass let's move that one back on the car a little bit to there let's try this out and that jumps a lot better so if the car is we still got a rolling issue here but that's okay so if the car is tumbling forward or or flipping backwards you can change the center of mass thing so it's in a better place for the car so another thing we should do is I noticed that if you rub the car against the side for example of sometimes when you'll scrape like a wall or something like that the friction is really hard for it so we'll create a new physics material here so here let's create a new folder called this materials and here let's just create a new physics material so new physics material and let's call this car chassis and then let's lower this value a lot to 0.1 just and for the friction here let's do minimum so it combines friction with another material I'll just do the lowest value and then for these colliders now on the car itself here go to colliders just assign this one click this little padlock at the top here to get this one this way you can select stuff here without it changing and then we can just drag this car chassis to all of these here so the physics material will have less friction on it now we probably won't notice too much of a difference let's disable that padlock again we won't notice too much of a difference but sometimes when new car new car lands like on the on the front or something like that it'll like bite and then tumble over but this way it'll slide a lot easier so if you land on so the chassis hits the ground instead of it catching with the friction and tumbling it'll just slide you instead so it'll be a little bit more fun to play that way ok let's start decorating the track a little bit now as well so we'll put some trees and things like that in and we've imported those before just as the meshes here you'll see in the project folder here you can see that we've got a bunch of meshes but we haven't set up any prefabs for it yet so we can drag those let's just drag a tree in here or all of them we can drag in so we'll select these four trees and drag them into the scene and they'll be pink at the moment because we need to set the materials so we've all created a material before that we gonna be reusing the common material so let's select those shift select them and drag this common material onto the material that's our trees basically and then we need some colliders for these as well and we'll keep that really simple as well by let's disable the car first and disable some of the trees so we only have one three year and here let's add a components let's create a child actually called colliders and then the first thing we'll do is just do a simple box Collider on this one and then we can resize the y-component here Center it up a little bit and then resize it and then make a little bit smaller maybe 0.5 0.5 then we just have to make sure yeah that's pretty good and then also what we'll do is we'll add a sphere Collider here if you are to fly up into the trees for some odd reason you probably will in this game then you want the sphere Collider up here so let's put that a lot higher and then resize it just the radius here so like that and again I won't make much adjustment to this we can fix this later on let's just have something in place here so it can collide up here as well and then I can press control C on this one to copy it hide it show this one and press control D and drag it into here and that one will share pretty much the same it looks so similar that that Collider should work for that one too I created a different layers well before so if I just select all of these and change this layer to props basically everything in them in the scene that's not the track or the car and things like that I just named the layer props if we want to separate those out later on so that's it and we're going to create prefabs out of these now so we can reuse the middle bit easier so in this prefab folder let's right-click and do create a new one called nature and then here let's just drag these trees in now and that'll automatically unity will create prefabs from these automatically and a prefab is a reusable object that we can use again and again so if I delete these from the scene now a really handy thing now is that we can just drag these trees in and since it's a prefab it will detect where the base of the tree is and it'll put it there so even if we drag one into here it'll detect where the collider is and put it up there so we can just drag a whole bunch of trees into the cenote now we've got some trees there if you don't want the mesh to show through like this there's a quick fix for it the real-time shadows are a little bit for hard edges it doesn't do so great of a job but we can actually change that if we block select all of these and then on cast shadows here just do two sided and that'll cut those whole you can also change the threshold but there's unities got a little bit of a problem with that as well it'll create some artifacts so this way you'll be a bit more expensive because it'll have to basically render the two sides of the material if performance is an issue try to solve it on some different way I'm happy with just setting the materials to two-sided no let's say under props here now let's just create a new folder here called trees or a new game object but we'll treat it as a folder and I can just shift select all of these and drag them into trees here and keep it a little bit more tidy in our hierarchy here so that's the trees them where we should also bring the tire stacks in so let's go back into the meshes here and then let's grab the let's see we can bring in stones actually we need this so let's bring all of these stones and just like this and for this one they'll end up here in the center of the scene hide the car again multi select these and drag this common material into the material and then here let's just add to all of these we can actually add just the mesh Collider because these will and then make them convex because the shapes are quite simple so now we've got a bunch of stones there that's good and then we'll just make prefabs out of these actually we need on this one we need to have the rigid bodies as well so add components rigid body and then we can set their weights of the the stones here as well so let's just set it to 200 kilograms or something like that and now let's create prefabs out of these as well so let's just drag the stones one by one and fortunately you can't multi select them and drag so you have to do them one by one that's it we can delete them from the scene there and now we can just start dragging a lot of stones into our scene the way we want to place them let's put a whole bunch here for example and you can scale them if you press that or you can scale the stones here so let's just throw a bunch of stones in here put some on this side as well so now that we're gonna let's try it out and I suppress plate you can you see that the stones really flipped out a lot and they flew all over the place and that's because I placed them into the ground you see that half the stones are in the ground so we can start by just selecting the stones and lifting them up but let's put them in the air for now just slightly above the ground like this so move these up a little bit as well and out to there and these stones as well let's just make sure that they're not colliding with the ground anywhere or the track and unity is gonna help us to place these stones a little bit so again let's just make sure that they're not touching each other or the track or the ground okay and now let's zoom out and let's press play and see what happens so unity drops it down it uses the physics engine here to just drop it into place and if we pause the game now we can do something quite nifty we can just select all of the stones that we done press ctrl C to copy those and then stop and let's just do something scary now we'll block select these and do delete on them so that now they're gone but we've saved them in their control see we've got them in memory so now I do control V and then you'll put those ones back and it will have placed them now in the correct position yeah unfortunately you lose the prefab reference to it so it's not maybe great for it for everything but for the stones to be placed it's quite handy and then here let's create a new props folder here call this one stones and then move the stones in today so now we've got a bunch of stones on the track let's enable the carb again and have a little look if we can collide with them there we go we've got stones now so we can collide to the next thing you could increase the weight of the stones if you want to have them a little bit stiffer or you can make them different weights for different sizes and things like that but I'll just keep it like this for now we can do the same now with a tyre barriers so if we go into the meshes here props here we have a tire stack let's drag that one into here to the scene let's put that at zero zero zero as well actually I hide a Corrigan and then we'll do the car wheel or the tire here at the same time so let's bring that one into here as well zero zero zero bring it up in a little bit so we can see it and now we need to assign the materials to these two so we'll shift select and do common material there we go and for this one as well for the tire stack let's add the let's add a mesh Collider for this one as well and do convex I'm just gonna stick our box Collider on this one so it doesn't roll all over the place we'll stop quite fast and they'll be able to you don't see the collider so if you want to change that to a different type of Collider or with a cylinder or something like that or that's fine now we can we have to add some weight to these as well so let's add the rigidbody to these two and the weights maybe of a tire stack could be 500 kilograms so they're a bit tough to drive into and a car tire maybe five and then here if I usually enable interpolation here to interpolates as well and then we should do that to the stones here as well actually stones and the reason why I do like to do that is because sometimes you can get a little bit of a choppy effect that the objects seem to stutter a little bit that's because they move up the physics rate and that's not necessarily in sync with the update rate a trick there is to just enable this interpolate and all that unity will transition those movements a lot nicer so it'll be quite smooth okay so we've got our tire stacks here now we'll create prefabs out of those as well so let's just drag the tire stack here into here prefabs and the tire as well and now we can start placing these objects as well so maybe in this corner here let's just drag tire stacks let's hit these and see what happens here we go that's good and we can duplicate here we don't need to take them from the project folder all the time we can take them straight here from the scene just view control D and then let's scatter a bunch of tires here as well so let's just bring a tire in here that's to go crazier will use the we'll use unity again to help us place these so I'll just press control D and put a whole bunch of them here and then we can select here multi select control D select the ease again control D like this let's put a whole bunch over here as well and here and there let's press play well let's have unity place those neatly for us pause it select all of these tires now and on control C let's play and delete these again it's a shame that you lose the prefab but it's quite nice to not have to place all of these manually so we can reap move these a little bit as well so they're not all in sync put a bunch of the tire stacks here as well or grab on ctrl D and slide it down to this corner maybe this is different from blender it's actually instead of shift each control be here in unity and it's e to rotate not R so those are two rotates you have to rescale yourself for this program so smooth control D our our e to rotate that's gonna mess people's minds sorry about that control dw2 move instead of G in blender okay and then let's grab a whole bunch of tires here control D you can spend a lot more time and make this a little bit more precise than I am but just do this quick now you can switch this one to a century if you want to rotate the whole list back like that which is quite handy so the old let's call them a new game object here called tires select all of these stacks under tires and then drag them into this tire and these as well and these that's it okay we press play again just double check that everything looks alright here's a thing that you'll probably run into see how those tires just blast it away I Drive you purely shoots those tires right out of the sky and here you have to change so light objects like that then that's why I actually created a layer called props here so let's go into this all these tires here shift select them all and put them into the props layer and actually I'll do that for these two prefabs as well props and the reason why is because in the project settings here I have disabled the collision between the wheel Collider and the props it's because it's not really coping with such light objects the wheel Collider puts some tremendous weird force on it so now it should be fine you don't really drive over those now as you can see the wheel collider you can't really drive over the wheels as such but we just have to keep it like that for otherwise we can't have it flipping out like that so that's the limitation as far as I know if you know we check how to get that to work properly let me know ok the final visual aspects that we're going to do is we're going to put the fences up now and let's drag this fence here into the scene there we go and we need to do this fence post as well move that one to there and then let's select these two and get the material so we'll drag this common material here okay so we need some colliders on these so let's just go here add a box Collider to this one make it a little bit less high so it clears the ground slightly move it up same for the fence post here add a box Collider and then we need rigid bodies on these so add rigid body let's put them at for maybe 20 kilogram with interpolate on and here we're gonna do as a trick as well because if we just press play now you'll see that these will fall over just like that and we need to have anchored those a little bit to the ground we can move them to the side here and test this out so for this we're gonna add a component here called a fixed joint and let's just set these values here to like 200,000 and now they should be in to the ground they don't fall over so if we enable the car again now you can drive into this fence a little bit and it'll keep you like from going through it but if I go fast into it okay I need to go even faster so gain some speed here POW then it breaks so the fixed joint will help your fence to keep the cars help it to not to drive off this ledge here for example but it'll if you go too fast into it and hit it straight on it'll break loose so that's how you control that so let's create prefabs out of these as well let's hide that on put them back into zero zero zero and here let's just create prefabs so we'll drag oh yeah after we do one by one so we'll drag this fence posts into here it's not in nature is it so there and then the offense itself and now we can start delete those now we can start placing the fence here so we'll just drag the fence into here rotate it hold the control key to snap rotate and then we do control D and we're only using as you can see we have the fence post like that so they link up let's do a test here actually let's create the whole bunch here so we'll do all the way to here and as we come to the edge here we can start rotating these a little bit so just go freehand a little bit control D or bring it to there rotates control D now you can switch this to local axis that'll help you to move it easier D okay and then when you get to the end here then you want to put a final post so we can just move this just a fencepost and rotate it okay and then I'll just speed up this because I'll place a lot of fences around the track so you can see what let's test it out let's enable the car and let's try to see if it helps us from going down yeah it helps us there but let's test again see if I can break them yep that works okay so that's gonna be it for this video I'm gonna just speed up and place these a little bit around the track and after that I'll learn I'll have to edit this video and have it out for you before I get lynch mobs off the internet because I'm too slow with creating these so I'll get this up and running for you and then I'll work on episode 5 as well we're gonna add some water we're gonna have the skid marks and the smoke and then start pumping everything up two into a little bit of a neater game as well some lap times and things like that I'm not quite sure which episode will contain which I'll keep pressing on and glad to have you with me and now hope you're enjoying this series thanks for all the feedback thanks for all their likes shares and thumbs up I'm really happy about it and I'll see you in the next video so take care until then [Music] you [Music]
Info
Channel: Imphenzia
Views: 89,361
Rating: undefined out of 5
Keywords: low poly, top down racing, car game, unity3d, unity, unity 2019, super cars, super cars 2, amiga, low poly racing, unity tutorial, wheel collider, car physics, tire smoke, skidmarks
Id: pYAgqrINfK4
Channel Id: undefined
Length: 57min 50sec (3470 seconds)
Published: Sun May 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.