Unity Car Controller Tutorial : Wheel Collider Physics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what is a welcome back to another video and in today's video we are going to be taking a look at how to create a car inside of unity so without anything further to do let's get into it before we get started I would appreciate if you could give this video a thumbs up and also subscribe to my channel and press the notification bell like and right next to the subscribe button so that you get notified every time I upload a video I would much appreciate your support and with that out of the way let's get going so the first thing I'm going to do is just tell you guys what my basic unity project is like and I've just created a new project I've just created a new script that has nothing inside of it except for the basic boilerplate code and I've also imported my cheap model that I'm going to be using this Jeep model can easily be found on the asset store and this is the actual model that I'm using on the asset store which is called car and it's a free asset so you can just download it and import it into your projects so with that out of the way let's get going so the first thing I'm going to do is actually create a 3d object which is going to be a cube this is the floor that we are going to have I'm going to rename our game object to floor and then I'm going to increase the scale to around 50 on the X and z axis and then after that I think we are good the next thing I'm going to do is just kind of create a new material so that our ground or floor in this case isn't clear and white and I'm just going to call it floor and not the best of naming conventions about we'll just stick to it and then I'm just going to squeeze the color of it and drop it on to our floor so the next thing I'm going to do now is actually create a new game object and this object is going to be our car or you could say a week or whatever you want I'm just going to stick with a car the next thing I'm going to do is create a child game object for this game object and I'm going to call this our model this is the game object that is going to be responsible for having our model and then after that the next thing I'm going to do is go inside my vehicles create final Jeep I'm going to drag my Jeep object underneath my motto and here we go as you can see that we have eg here right now so the next thing I'm going to do is just position my Jeep at zero zero and zero inside of my parent object and then I'm going to move my parent object one unit up on the y-axis we can focus on the car and as you can see it is right now quite small so the next thing that I would like to do is actually just go back to my jeep model and increase the scale toward tenth and I think this would be a good skill for the car yes looks good and once we've removed the animator the next thing I'm going to do now is actually define the real structure that our vehicle is going to have as you can see now that our model has four wheel objects and this is a good thing because in order for the method I'm going to show you today you need to have an object that has these wheels not attached to the actual object and they are just separate objects so the first thing I'm going to do is under my Jeep object I'm going to create a new empty game object I'm going to call this wheel models and then I'm going to copy all of my bills and I'm going to drag them under wheel models before we can do this what I have to do now is actually go to my Jeep model and then unpack prefab completely so once we've unpacked our Jeep model the next thing I'm going to do is actually copy all of our wheels under real models object and the reason we do unpack prefab is then unity will allow us to actually I did the children inside of our game object so once we have our read models I'm just going to copy this I'm just going to copy our game object and I'm going to just duplicate it and this time I'm going to rename it to wheel colliders and then underneath of this I'm going to select all of my wheels and I'm going to remove the mesh renderer from them as well as the mesh filter and I'm going to keep the wheels as they are here with the mesh renderer and the wheels attached to them so now we should have two game objects one is responsible for holding our wheel models and the wheel colliders game object is going to be actually responsible for holding the colliders for our wheels so now the next thing I'm going to do is select children objects of our bill Collider parent object and I'm going to add a component to them and this component is going to be wheel Collider and with this done we should have some kind of wheels on our vehicle okay but before we get going with actually changing the wheel colliders what I'm going to do is actually go to my car the main car object and I'm going to add a rigidbody component to it because in unity for an object to have physics applied to it it needs to have a rigidbody component and then the next thing I'm going to add to this is I actually going to be our car controller script so I'm just going to go back to my assets and I'm going to drag over at the car controller script as of now we don't have anything in it so you can just create a new car controller script now we are ready to go back into our real colliders and work on them now we can see these pretty huge wheel colliders so now the time comes in where we actually fixed it now keep in mind that the values that I'm going to be using right now for a real Collider are going to be applied to all four wheel colliders that we have and these values may not be the ideal values for whatever we go that you are going to be working with depending on what kind of rigidbody values you use but for the if you use the same rigidbody values as I am using and then say use the real Collider values that I have defined the car is going to work so now what I'm going to do is actually change the wheel Collider settings that we have so for the mass the mass I'm just going to keep at 20 the radius I'm going to make it zero point zero one six the next thing I'm going to do is I'm going to change the wheel damping rate and this is going to be how smooth the car handles jumps or how smooth it goes over bumps and I'm going to keep it at 0.25 and then the suspension distance which is going to be 0.03 this is how much the suspension can move or the wheels can move up and down relative to their set relative to the suspensions a resting point then we can leave the workforce at point distance but for the center as you can see the wheels right now are quite low so I'm going to move them up a bit on the actual suspension and I'm going to move them about 0.015 up and now you can see that they align quite good with our cars wheel the next thing I'm going to do is going to change our spring setting and I'm going to make it a hundred thousand and then for my and I don't know why this isn't working but this should be your damping value for the spring and this is going to be two thousand and then the target position for the spring is going to be zero point five the next thing I'm going to do is just let you guys know that you do not have to change le of these values and they are some advanced use cases that you would use these and for example making the car drift changing the physics of carpet for our purposes or making just a basic curve I think these settings would serve as good so with that done what I'm going to do is just copy this component go back to all of the other wheels that I have and then I'm just going to paste component values into them and voila now we have the wheels all set up correctly and the car has a rigid body but if we right now play the game you're going to see that the car is not going to work like it's supposed to and as you see it just shoots off and that is because the wheel colliders do have a Collider on them but they don't have anything to add to they don't have a body or they don't have anything to collide with so they just go bombard any word and data school bombarding elsewhere so the way we could fix this is that to our Jeep model what I'm going to do is actually or not to RG model but act to RG body I'm going to add a mesh Collider and I'm going to make this mesh Collider convex and now you can see that our G body has a Collider and so do our wheels so now if we play the game we're going to see that the car is still going to fly away and the reason for this is that the mass of our rigidbody is really low as compared to the actual sprague force that our car that each wheel of her car exerts it's just flying away so to fix this we can just increase the weight on for rigidbody and I'm going to keep it at 1500 and we don't have to like bother with other values you can play with them and see how they change the dynamics of your car but these should be pretty good and as you can see now our car is just moving and as you can see I can keep moving the car and it doesn't fly off and if you just keep them up you're going to see that the tire or the wheel colliders in this case move down that means that yeah the suspension is working okay so with that done I think the part where we have to actually set up our car is done and now we're go to the good stuff with this coating so the only script that we are going to be using is actually our car controller script and this is the script that is going to actually encompass all of our functionality so the first thing that I'm going to do is actually create in a public strut and for those of you don't know what struct an easy way to think what a struct is is you can think it's a class that just has properties in it and it has no functions or methods associated with it and then inside of my struct I'm going to define three properties which is which are all going to be public one is going to be our game object and this game object is going to be the actual real model which we willing to call model the next property is actually going to be a wheel Collider which we're going to call a Collider and then the last thing is going to be in axl for a wheel and I'll create an axle pretty soon but we can just call it an axle and an axle would be an easy way for us to differentiate between what are the front wheels of the car and what are the back wheels of a car so I'm just going to quickly define an enum and this enum is going to be called axle and then the enum is going to have two types one is going to be front the one is going to be river and then this should pretty much be all the two custom classes that we need one is what our axle is and now having the axle innum the advantage that we have is that we can actually define the type of wheel a wheel is whether it's the front wheel or a back wheel and then we can do different stuff with it like only front wheels can turn only make a car two wheel drive so give power to only the back wheels or pick a car four-wheel drive change the gear car goes into four-wheel drive and then you can just do a lot of stuff programmatically which we're not going to be going through this tutorial that is something that you should guys can do ok so the next thing I'm going to do now is actually define all of the properties that we are going to need for our car controller so first of all we are going to define a property and since this is going to be a private property to make it show up in the inspector I'm going to market as a serialized field it's going to be a floor and it's going to be our max acceleration and I'm going to default it to 20 point so the next is also going to be a private float field and this is going to be our turn sensitivity or how well our tires respond to our inputs to turn or our horizontal inputs and I'm going to default this to 1.0 F the next thing I'm going to add is going to be our max tear angle and this is how much each Wilkin so that's going to be our max tear angle and this is also going to be a float and I'm going to default this to around 40 5.0 F which is quite a lot for a car and then the next thing I'm going to do is actually create another sterilized field and this is going to be a private list of wheels that our car has and this this wheel class is actually referring to the struct that we have and then we're going to call it wheels and then that should be pretty much it and then the last thing we're going to do is actually have two private flora variables what is going to be our input on the x axis and one is going to be our input on the y axis and then the last thing or the final thing we need as of now is going to be a reference to our cars rigid body which is our B so with that then the basic setup should be done so now the next thing I think we should work on is actually making the car accelerate so I'm going to first define a new method and this new method is going to be a private method and we are going to call it late update and this is the method that is going to actually be responsible for making our car move so I'm going to call it late update and the reason that I'm using late updated not update is because physics calculations need to be performed and late update it's just something recommended by unity and then inside of here I'm going to create call a new method which I'm going to call move and then I'm just going to now create a new method which is going to be move and not on animator move just move and then inside of here we can actually define the behavior or define the logic for actually applying force to our wheels so I'm just going to create a for each loop and then I'm going to call for each wheel in our wheels list we all dot Collider and then we are going to give it a torque so I'm just going to give it a motor torque and we are going to make it equal to our input on the y-axis times our max acceleration times a constant of 5 you can think this as of a force constant times time to Delta times or that if we drop any frames due to some performance issues of the acceleration adheres to that change and with that I think the move function is done but if you play the move if you play the game right now the car isn't going to move because for now our input is just going to be zero so now the next thing we you have to do is actually get the input so inside of my update function I'm going to create I'm going to call a new function I'm going to call this get inputs and then after this I'm just going to define a new function and I'm going to call it get inputs and the inside of get inputs I am going to first take our x-axis and I'm going to blue input dot get access and you can get access roll if there's no input smoothing on it but I'm just going to go with get access this way it is just going to do some pre smoothing on the values that we get and the movement isn't going to be too jerky and then what I'm going to do is actually pass in the axis name so in this case it's it's the x-axis or an input X I'm going to toss in horizontal and then I'm going to pass an input Y which is going to be input dot get access and this is going to be vertical okay will lasso when I we were getting input to in our update function if I do command save and we go back and don't just try to run the game right now because you have to do some stuff and the stuff we have to do is first actually change the script because the wheels are trying up and the reason the wheels aren't showing up is because even though this is a serialized field our actual class or wheel class isn't marked as you realizable if you create a class that does not inherit from monobehaviour and to show it up in the inspector you'd have to actually market as a serialized field and then we can just go back and this time I think the wheels would show up [Music] and for some reason it's not and the reason is because I marked this at serialize and that's you realize field and the reason for that is because we are not using the proper namespace to actually have access to our serializable tab and now we can mark this class is to be realizable go back and this should be good yep and now you can see that the wheels actually show up so I'm just going to add four wheels I'm going to open all of the elements I'm going to go to my car go to the wheel models copy all of them go back to the car logged instructor first at copy or then copy all of my or not copy all of the wheels drag two bills one by one so this is going to be the first variable second rear-wheel front first wheel front second wheel and then I'm just going to copy over the wheel colliders now so and if you just drag over the game of it is automatically going to get the wheel Collider component off of our game object just to make sure to actually have the proper colliders associated with the wheels and then just drop them in drop them in and then change these axles to we're and that should be good now we can do command save and let's see if the game runs four cars down and you can see that the car is moving if you press W it goes forward we press s it goes backward forwards backwards there's good braking option to it too as you can see okay so that's one thing down okay so the next thing we can work towards is actually making the wheels turn and then the last thing that we're going to do now making the actual wheel animation shoulder to make the wheels showed as turning and spinning so inside of my late update function I'm going to create and or I'm going to call a new function now this is going to be turn and not turn sensitivity just turn then I'm just going to call a new function private void turn and then I don't turn and then and inside of our turn function I will again I'm going to create a for each loop and I'm just going to call it the same I did for our mole function which is reveal in the bills and now each will we're going to do some calculations with it so the first thing I'm going to do now is actually check if the wheel is the front wheels I'm going to do if and I'm going to do wheel dot axle is equal to axle dot front which means that this is a front wheel and we should do stuff with it and if it was a front wheel the first thing I'm going to do is actually calculate the steering angle for the wheel so I'm going to do is steer angle and then I'm going to move the steering goal we're going to get it with the horizontal input that the user provides times D turn sensitivity which is just how sensitive the turning is relative to our input and this is what we had defined above so you can change it and this may increase the sensitivity or decrease the sensitivity and then the last thing is going to be our max steer annual that's the maximum we can steer now the next thing I'm going to do is actually make the steer angle it goes smoothly between two handles because as if we don't do this and let's just do it actually where I'm not going to actually smooth out the transition from one angle to another and I'm just going to do wheel dot or I'm just going to do our wheel dot Collider that's steer angle and then I'm just going to assign our steer angle to it but if you do this what you're going to see is that it's not going to be what we want because if you go back we run the game you're going to have this weird jerky behavior where oh you can't see the wheels turning we can go into fire frame board then select one of the front wheels and you're going to see that the the turning is not that smooth as you can see so the way we can fix this is just go back to the place where we actually assign our steer angle to our Collider and I'm going to just use a function which is not half dot work and Lord just like linearly interpolate between two values and the first value that it is going to go from is going to be real dot Collider don't the steer angle to our new steer angle and then it is going to go at 0.5 s 5f distant work well you can be between 0 and 1 and this should pretty much actually smooth out the turning for us a lot actually and if we don't us you're going to see that the turning is really smooth now and it goes back as you can see it goes back now to so now it's actually good okay so with the turning taken care of the last thing we have to do is just work on actually making our wheels enemy so I'm just going to create a new function and I'm going to call this animate wheels and this is going to be called inside of our updates function and then I'm just going to create a new function and this is going to be private void and I made wheels and the way we're going to animate our wheels is I'm going to take ahead individual colliders rotation and position and I'm going to use that position and rotation and define the actual wheel models position and rotation so then again we're just going to create a for each loop and this time it's going to be and if you want to autocomplete some stuff and you are wondering how I want to complete my code where I just type for each and that it does everything for me just select whatever you want and then click do double tap and it would autocomplete the codeblock for you I'm going to do four wheel in wheels and with that I'm just going to first define two variables which is one is going to be a quad-core Tinian and this is actually going to be holding our wheel rotation and then let access going to be a vector three which is going to hold our wheels position and then the next thing I'm going to do is the wheel Collider dot get world pause and this is going to give us the wheel colliders position relative to world space and then I'm going to take the output of this function and since we get two outputs from this function I'm going to put them into the pause and the brought variables respectively and now with this we are pretty much done so now the last thing I have to do is do the old odd model or wheel dot transform wheel dot model dot transform dot position and I'm going to assign it the position for Collider and then wheel dot model dot transform dot rotation and I'm going to assign it the rotation of our model I'm going to do command C and we can go back we can change from our frame mode to shaded mode run the game and now hopefully the wheels should work and what they do as you can see the wheels are turning they move you can see our car has limited traction and the wielders just turning haphazardly not the best engineering decision and you can see that the car turns to but one issue that's not like a parent is that our car flips which shouldn't be the case for a car and as you can see that the short term just makes the car flip so the easy a solution to this would be to just have the reference to a rigid body and change its center of mass and as you saw it actually find a rigid body so I'm just going to do RB and I'm going to for my game object just do yet component and then the gap component is just going to be a rigid body and this getcomponent function is just going to take the rigid body that is attached to the current object and then assign it to our RB variable and then once this is done the next thing I'm going to do is just do underscore RB dot center of mass and I'm going to equate or make it equal to another variable that we're going to define and this is going to be uh see realized I don't know why autocomplete isn't working lets you realize field and this is going to be a private vector3 and I'm going to call it Center off sorry for my weird typing I just got a new keyboard and center of mass and I'm just going to assign the center of mass to it you can do command save and then we can just go back and actually define our center of mass for a car and the values that work for me were negative zero point three on the y axis and that was pretty much what I needed you can tamper with the Y value and hopefully get a value most probably closer to what the value I have and then your car is not going to flip so now we actually turn the car the turn car turns moves if you guys would like to see a future tutorial in which I should show you how you can actually make this car drift drop a like on this video also don't forget to subscribe to my channel and press the notification I can so you get notified every time I upload a video and I would much appreciate you dropping a comment and telling me what you would like to see for future videos so with that out of the way I'm going to end this video here and I'm going to see you guys in the next one have a very awesome day
Info
Channel: Hussain Mustafa
Views: 30,027
Rating: undefined out of 5
Keywords:
Id: uSr5IfzclP8
Channel Id: undefined
Length: 28min 35sec (1715 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.