Wheel Physics in Unity 101 Part 1.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] before you can start creating any racing game where you're going to be relying on physics you need to understand what's underlying the system and it's not only the overall physics system but what really is and literally is driving it are the wheels of your karts and the physics that is on those wheels and your way you set that up is absolutely fundamental to getting your game correct now if you've had any experience using the wheel colliders and the vehicle setup in unity you'll know that it's seemingly very unpredictable especially if you don't know where to start or what exactly you're trying to achieve so in this lecture we're going to go right back to basics and look at just a single wheel let's jump right into having a bit of a play with the wheel physics system that is in unity what you can see on the screen here is a tire or a wheel that I've brought in and also a cube now before we get to those let's just think about unities physics system or unity in general about the actual physical systems you put together in unity they're all based on the metric system so measurements are in meters weights are in kilograms and speeds and things like that have meters per second so this cube that you can see which I've added in here so in the Hokie I'll just right-click go on 3d object and add it in a cube this cube I haven't changed the size of when it is scaled at one by one by one it is one meter by one meter by one meter this tire which is in the unity package attached to this lecture as a resource i've unzipped it dragged it into the assets so that you can see it here and then dragged and dropped it into the scene now if you're following along you might like to pause the video and make this scene up you'll also need quite a large ground plane see that mine is massive because you don't want to drive off the edge a little later on okay now you'll only need one wheel not two so I'll just get rid of my second one that I created now the reason I have this unit cube in here is to compare the size of my tire and if you think of your average car it's probably going to be about two and a half to three meters long and probably like 1.5 meters high depends on the type of vehicle that it is obviously but you want to make sure that it is accurately sized with respect to what unity expects the reason being is that as you start applying forces and weights and all those sorts of things to your vehicle it's going to make sense if you can keep it in real terms and therefore you don't have to be applying unrealistic forces to things to get them to move forward if they're all scaled properly the other thing you need to check is on your wheels most importantly is that you also have this z-axis going forward so if you click on your wheel and hit the W key to bring up the axis system on there you can see that this tire has the Z the blue axis going forward now the red axis doesn't matter as long as it's going out to the side in either direction usually when you do bring someone else's car model in the inside of the tire if we just turn around have a look doesn't actually exist so it's kind of saving space on modeling and extra polygons that might be in your scene so if you were to use this same tire on the other side of your car then you probably would flip it over and therefore the red axis would be facing out in the other direction let's add some physics onto our wheel and have a little exploration of what we're dealing with so the first thing we're going to do is just turn off the cube so I'm ticket in the inspector we will use it a little later on select your tire it needs to have a wheel Collider on it so add component look for wheel collider and add that in okay so the wheel collider if we try and play at this point if I just press play and have a look in the console we're going to get this error saying that the wheel collider requires an attached rigidbody actually it's not even an error it's just a warning so things will still run sort of so we've got this wheel club we can't actually see it or do anything with it as far as setting its properties so let's add a rigidbody to our tire as well so search for a rigidbody and add that component so you can see I've got both of those as soon as you add your rigidbody you will get this kind of display on your wheel okay you've got this big outer circle there's a little sphere at the bottom and then if we have a look over in my camera view you can see this orange line I'm going to explain all of these things in a moment the wheel needs to have a rigidbody as you just found out usually the rigidbody for the wheel is going to be on the car's body not the wheel itself but we don't have that at the moment so we've got everything all in one here that's fine this is still going to work and it's a good little time to experiment with this Collider without confusing the matter by having more wheels and a car body and all this physics stuff just going wrong and you've got no idea so that's why we're just looking at this single unit wheel so exactly what are we looking at well what I'd like you to do once you've just added those two things with all of the default settings in it is press play let's have a look what happens okay where'd your tie go obviously up you saw it bounced it come back down to earth and it's flown off again who knows where the reason being is that the physics system is sending it back up in the air it's got this a massive amount of bounce on it now why is that on the default system okay well we think of a car as quite a large heavy object the suspension system that's built into this wheel Collider which is actually represented by this orange line here it's the equivalent of a car suspension spring now imagine how much weight that that is holding the car in this case is basically the rigid body that we have attached to this wheel let's have a look at the properties of our rigid body and you can see if you have a look-in inspector that it's mass is set to one now that is one kilo one kilo on top of a compressed suspension spring that's meant to lift a car imagine how far it can fling one kilo and you're actually seeing it happening there when you press play on the wheel clutter itself it has a mass of 20 and that's fine the average tire I guess is around 20 kilograms but in this case it's not really helping much with what's going on with that springing action of bounciness so what we can do instead is change our rigidbody to something more like a car which is about a ton or a thousand kilograms so with that Set let's press play and have a look how our tire behaves now you might get slightly different results from me and your tire might actually go through the ground plane at this point don't worry if that happens okay did you see what happened that time the tire fell and bounced just a little bit as though there was a spring involved because it oscillated before it came to rest it didn't come to rest at the bottom of its mesh let's have a look you can see there it came to rest on that little sphere butts at the bottom and on the bottom of the circle that's around it so is it sitting on the circle or is it sitting on that sphere what's actually sitting on the circle the circle defines the outside of the wheel so stop playing what we're going to do with what we've got here is just get yourself into a good front on view to your tire the wheel collider we've got the massive 20 underneath that we can set the radius so I think it's about 0.35 is about the right size okay so if we size it to that you can see that it's better eye size though it's not in the right place so you can use the center section to actually just move it up a little bit until it's aligned nicely with the mesh okay so now what happens when we press play it's going to fall to the ground and it has a little bit of Bounce from the suspension okay so you can imagine a much heavier object like a car or something sitting on top of this now being dropped to the ground and then it bouncing just a little bit on its suspension spring let's now take a look at our suspension so the suspension system as I mentioned before is pretty much this orange line going through the tire now to see it over in the scene it is kind of there but it's buried inside the mesh if we just change our mode to wireframe that we're looking at it with can see that much clearer take a look at where the green circle around the outside is aniline through the middle with respect to your spring it's currently sitting halfway along so let's press play and see where it ends up and notice when you first press play if I can quickly pause you'll see where the collider sank to so it went immediately from the center down to the very bottom and then if you continue to play as it hits the ground you're going to see it bouncing on the suspension so let's just go like that okay did you see what happened there so it bounced up and down until it came to rest at about that middle point of your suspension bar so what why is it doing that well it's doing that because of the target position that is set over in your wheel collider if we go over to the inspector and have a look at that target position which is down in the suspension spring area you'll see it set to 0.5 that means that it's trying to get the Green Line halfway along this suspension as it is reacting to physics and that's when it's going to finally be satisfied that it's come to rest and will stop bouncing now if we set this target position to one which is right up the top there let's have a look press play and what do we get pretty much no bounce whatsoever okay if you imagine that this Collider is sitting at the top of the spring and it hits the ground the initial reaction of your force is to try and go up when it hits the ground and it's got nowhere to go there's nothing up above there for it to slide up on therefore you've got a very very sort of damp spring that gives you very very little bounce okay let's try and set the target position to zero now you've got all of this spring to move up on and the result well it's a much bouncier tire as you saw there now in both of these situations you can see that the tire isn't actually sitting on the ground now the reason for that is it's all about this circle around the outside it's not aligned with your mesh therefore it's not going to come to rest on its visual bottom so just to change that once you've got your settings let's say you do want it to be as bouncy as possible in this particular scenario then you need to actually change the center of this whole system and move it up again so change that Y value in the center until it's aligned with your wheel again now when we press play you'll have your bouncy tire bounces a bit and then it comes to rest sitting on the bottom okay how about you pause the video now and you try and get it to work if you have your target position at one to get it to sit on the bottom okay how did you go with that let's set our target position up to one that means our Center needs to come down to fit our mesh press play it will be a very damp bounce as you saw there and it's come to rest you know pretty much on the bottom okay let's set our target position back to 0.5 and this time what I want to do is modify the length of our suspension string and you can do that over in suspension distance so it's currently set to 0.3 if we increase that to say 1 you'll get a much longer suspension spring which means you've got the potential of an awful lot more bounce for it to move along and it's set halfway along that get bounced up in both directions okay so we want to position this again in the middle of our tire now with all of that extra suspension spring let's have a look what we get on our tire so if we press play okay we're still getting it moving and it's still giving a bit of spring but I don't think it's actually changed very much at all except that it's sort of when it does hit the ground it actually has a lot further to go down before it might hit the bottom of the spring so have you modified much of the bounce by doing that well it is a little bit bouncy ER but you know not much and the reason being is if we have a look at our other settings for our spring is we have like an amount of spring which is as you can see 35,000 which is a huge and we also have this damper factor now it's going to be the damper that is going to dampen that bounciness so if we turn this down to say I don't know let's put it at a thousand and then press play we'll get a much bouncier tire OOP and and it obviously has just fallen through the ground okay so again unpredictable things that you get with the physics system there's a lot less chance that it's going to fall through the ground once you have four tires on there and in fact when we remove the rigid body it will fix all these little glitchy things because you not meant to have the rigid body on the actual wheel itself right so what happens if we put our dampening what was it forty five hundred and turn our spring down to let's say a thousand okay so that's considerable let's press play and have a look what we get okay so our Spring doesn't have as much force and it's a much gentler into it because again you've got this damper going on which is much greater than the downwards spring so let's change our damper down to 100 not while we're playing and have a look at the effect okay so that had the result of giving you more bounce before the tyre disappeared and it actually gave you much more range what was going on with the particular tyre let's set our spring back to 35,000 and our damper back to 2500 which seemed to be pretty optimal values which I'd say is why unity has them as the defaults right the last thing I'm going to show you in this lecture is the force applied point distance what does this do and what on earth is this little sphere at the bottom well when we start turning the tire and adding forces to it this sphere is where those forces get added to so if you grab your force point distance and actually change it you can see that that's changing with the location of this sphere so if imagine you're trying to like push a tire along a road where are you going to add force well if you're pushing with your hand I guess you're going to be doing it at the top we're going to actually be pushing it from the bottom along the surface of the road so when we apply forces to this tire we actually apply them where this is if you were to try and push the tire by adding a force in the center you're not going to get as much kind of leverage on the tire so if we now just let's move this back down the bottom here then we ready I'm just going to set this back to 0 we will leave that there but you of course can play around with this as you like so does the basics of the wheel colliders suspension when we come back we're going to look at the other properties as well as start programming for these thanks for watching please support the development of more superb online learning content by subscribing and as always visit holistic 3d comm to learn more about awesome games development books and tutorials
Info
Channel: Holistic3d
Views: 31,757
Rating: undefined out of 5
Keywords: Unity, Unity3d, tutorial, wheel, physics, collider, drive, car, kart, racing, lecture, driving, networking, multiplayer
Id: mnAEeE3FcvA
Channel Id: undefined
Length: 20min 21sec (1221 seconds)
Published: Tue Nov 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.