Unity3D How To: Driving With Wheel Colliders

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we're going to be working out of unity 20 18.1 with the template standard 3d render with graphics extras which is going to give us post-processing effects and go ahead and initialize some graphic settings for us so while you're watching this your main goal is to have the ability to drive a vehicle around your own unity scene and to do that you're going to have to work with wheel colliders a vehicle asset and a couple of scripts to help you drive around the core component of this is wheel colliders and if you've ever worked with this unity component then you know it can be very frustrating to get right there's a lot of physics involved and a lot of parameters that you've probably never heard of or they're related to a more deep physics understanding and it can yield some very interesting results to say the least and we'll go ahead and go over some of the ways to fix some of these problems a little bit later in this tutorial so first let's go ahead and download a free vehicle asset from the store and apply wheel colliders to it and the one I'm going to be downloading is called low poly destructible two cars number eight okay now this is the starter scene that unity gives you in 2018 this is one of the templates what I want to do is just duplicate the sample scene that they give you call this test and this is going to be the scene that we're going to be working out of so we'll jump over the test and then delete the example assets and then click on the main camera and disable the simple camera controller because we're actually going to be creating our own camera controller in this tutorial now I'm going to find the model from the vehicle asset that I just downloaded and I'm going to drag that into my scene and what I did was actually change the scale factor to 0.25 and we could see that this model comes in without any bumpers or wheels and the reason for that is probably because the Creator had customization in mind but I'm going to go ahead and find those bumpers and wheels and place those accordingly and for each one of these objects that I'm dragging in I've changed the scale factor to 0.25 just so it fits the main model so I brought the back bumper on there and now I'm bringing you the front bumper and then I need to bring the wheels so the will comes into the middle of the model and what we need to do is actually just find the position for each one of the wheels so this one could be point two point two or point two point one and point three seven five and then I'll go ahead and name that wheel my front passenger wheel so front passenger I'll duplicate that and inverse the x-position so it goes over to my driver's side and then I'll call this my front driver wheel I'll duplicate this and this is going to be my rear driver wheel so rear driver and the position of this is going to be 0.2 0.1 and negative point three four five it looks like a good position for that and then I have the rear passenger wheel which again I'm going to inverse the exposition of so I can bring it over to the other side okay so that is a good position for each one of those wheels I think and I think at this point what I want to do is create a prefab for this vehicle so I don't have to worry about moving those wheels around every time so I'll create a prefabs folder in my main assets directory and I'll just drag this car and to my prefabs now the funny thing about the wheel Collider component is it requires a very specific hierarchy structure so I'll go ahead and describe that in minimal detail right here what we want is a parent object for our wheels and that's going to have the geometry of the wheels and then I'll want another parent object for the components alone so the wheel Collider components alone and I'll drag the geometry of the wheels into the wheels component and then actually instead of doing that way I'll duplicate the wheels parent object all that Wheels wheel colliders but I've duplicated a lot of geometry so I just want to go in and delete all of that [Music] okay and it looks like each one of these have an animator component on it so I'm gonna delete that because we won't be needing any animator controllers in this tutorial okay so now I have a bunch of empty game objects in the place of each one of my wheels now each one of these empty game object needs to have a wheel Collider component corresponding to the wheel that it's located at so for instance this front passenger is going to have a wheel Collider component and that wheel Collider is for the front passenger wheel okay now there's a lot of settings in a wheel Collider component and there's a lot of physics involved that I won't have time to go into but I can say that the majority of you're tweaking is going to be in the spring and damper settings so we have thirty five thousand and forty five hundred is the default these other settings for the most part are okay as the defaults and then I'll probably be changing the mass and some of these other settings as well just based on some of the trial and error that I went through before this tutorial so what I'll do is I'll define each one of these as I've had them written down and the mass of the wheels is going to be 100 the radius will be point zero eight the wheel damping rate will be one suspension distance is point zero a force at point distance can be zero the center can be zero and then the spring and dampers the spring and damper is largely dependent on the mass of your vehicle for this I'm going to set spring to ninety thousand and damper to nine thousand the target position is going to be one okay and we can apply this wheel Collider component to all of our wheels so I'll copy that component click on all of the other wheels and then paste the component as new so now we have all of our wheel wheel Collider components set up and at this point we would want to create a ground so I want to drop the vehicle and see if it actually lands properly as you would expect so let's go ahead and create a ground object so I'll make this can be a cube I suppose and that's going to be placed under the vehicle and then I'm going to find a material to place on this ground and I'll probably take something from the example assets so if I go into their materials I see a ground mat I can just place that right there okay so this looks pretty good now I want to just press play and see if the vehicle drops which it shouldn't at this point because there's no rigid bodies involved okay so going on I want to repeat the structure of this vehicle so we have our base model here as a child we have wheel components which are the geometry of the wheels then we have another parent object four wheel colliders which corresponds to each one of the wheels and it looks like I accidentally dropped my ground into the child of that wheel salt move that okay so again the wheel colliders parent has only the wheel collider components and this is actually necessary to make this system work properly okay now what we want to do is define our rigid body for the vehicle so on the vehicle we add a rigid body and the mass of this car I set to 1500 and that plays a big part with the spring and damper and some of the other settings in a wheel Collider component so 1500 for the rigid body and this alone still isn't enough you'll get some really weird behavior if you just leave it like this so we want to add a box Collider to our system as well and what I'll do is I'll just move these walls around I'll move the bounds of the box until it looks like it fits pretty well and so that's good enough for me I think now if I bring the car up actually before I forget we want to make sure we have a box lighter on the ground and we do so now I want to just pick the car up and hit play and I just want to see the car fall onto the ground and that way we know that our system works and it looks like that fell on just right so the setup process for this tutorial is actually done we have our wheel Collider components and our wheels all set up the way they need to be now we just need to create a car controller script to work with the wheel Collider components and then we'll also create a camera controller to actually follow the vehicle as we drive around so now we want to jump over to our scripts folder here and create a car controller we'll call this simple car controller it's just to keep with the naming conventions in this folder it's a simple car controller and then go ahead and open that up and I usually just get rid of the contents of the script first thing that's just a habit let's define some member variables for this car controller what are the things that this car controller would need to keep track of so we need input let's create a float for horizontal input and we'll create a float for vertical input horizontal input is going to be your a and D keys vertical input will be your W and s or you can think of it in terms of arrows horizontal would be your left and right arrow and your vertical input would be your up and down arrow now we want to keep track of the steer angle so what's our current steering angle and that's going to be modified by the horizontal input I also want to have public references I want to know where my wheels are where the transform wheels are and where the wheel collider components are so what I'll do is I'll create public wheel collider and I'll have one for front driver wheel and rear or I'll say front passenger wheel I'll also have wheel colliders for the rear versions of those two rear driver wheel and rear passenger wheel and also I want the transform versions of all of those as well because I'll need to modify the position or really I'll just need to modify the rotation of those wheels so I need the transform component so I'll need the transform for front driver and I'm marking this with T and W so throughout the script I'll know if I'm dealing with a wheel or if I'm dealing with a transform so I have front driver front passenger and then also the rear versions of those as well so rear driver and rear passenger okay now I'll also want a public float for my maximum steering go so basically this controls our steering radius so how fast can we turn or how fast could we do a u-turn in our vehicle and I'll do I'll set the default of this variable to 30 so 30 degrees is going to be our maximum steering angle so one more variable here and that's going to be describing how fast we can go and we're going to call this motor force because it's actually going to be modifying the motor torque variable in our wheel Collider component I'll initialize this to 50 and as you'll see that's a pretty average speed for which it would expect a car to be going so that's it for our member variables now we want to define some methods so I'm just going to draw out the skeleton of this class we'll have get inputs we'll have a way to steer so steer will have accelerate will have a way to update the rotations of the wheels and also the position because if you think about a car being suspended in midair the wheels would be hanging a little bit so we actually will need to modify the position of the wheels and not just the rotation so I'll have a method to help us do that so update wheel poses and a helper method for each individual wheel so we don't have to duplicate a certain block of code for every wheel I'll have update wheel pose and that's going to take a wheel Collider and a transform if you're curious about my variable naming conventions what I typically do is an underscore leading the variable name if it is a variable that's local to a method so I know that any variable that I use with an underscore can be accessed from anywhere outside of that method then I have the the M underscore that's a member variable for the entire class so if you're curious about why I'm naming these variables the way I am that's why I'm doing it and it's definitely not required okay so we have that method and then we'll have a method for fixed update that's where all of our code is going to be going into so every fixed update cycle we're going to get input we're going to steer we're going to accelerate and we're going to update the wheel poses okay so let's just start from the top what's going to be going in our get input function all we need to do here is define the value for our horizontal and vertical input remember that is the WASD keys or the arrow keys so M horizontal input is going to be equal to input get axis horizontal as a string that is getting the input from unity so unity has an input method called horizontal if you're not aware of it yet let me go and show you really quickly what that is if you go into edit project settings and input you have these axes defined for you these are input axes so you have horizontal and vertical are showing up as your first two so horizontal uses left and right or a and D so left and right arrow keys or the A and D keys so if you're curious about what that is that is where it is defined in unity by default okay so back in our script we want to define the vertical input which is going to be equal to input get access as you might have guessed we put vertical right there now we want to define our steer angle so our current steer angle is going to be equal to I can't remember if I named that steering or steer angle so max steer angle and steering angle okay so steering angle is going to be equal to we could say max steer angle x in horizontal input and that should work for us and then what we want to do is update the steer angle for our wheels so we can access the wheel Collider components from these public members that we defined and normally you would want to check if this stuff is going to be null or not because we may or may not define it in the inspector in which we should throw in throw an exception but I know that I'm going to be defining those so for the sake of this tutorial I'm not going to go into exception handling so what I want to do is just save my front wheels so front driver wheel dot steer angle is equal to am steering angle and also my front passenger wheel not steering goal is equal to my steering angle okay so that handles steering for us now we want to deal with acceleration and I'm only going to be applying acceleration to the front wheels so that would be emulating front-wheel drive but we could also apply it to our rear VAP our rear wheels to emulate rear wheel drive or we could apply to all of our wheels to emulate all wheel drive but I'm just going to do the front wheels for this tutorial so we'll say the front driver wheel dot motor torque is going to be equal to our vertical input so that's our up and down arrow or W and s Keys so that's going to be our vertical input times our torque so our motor force is what we defined down here is a public ferry public variable we'll do the same thing with our front to passenger wheel dot motor torque equals the vertical input times motor torque I'm sorry I keep saying motor torque but that's actually motor force I'll make sure we get that right okay so that's our acceleration that handles everything that we need now we want to update basically how the wheels look so this will update the rotation of the wheels to make it actually look like the wheels are driving so they're actually spinning around and if the car suspended then the wheels will drop a little bit or if you run over a rock the wheels will go up into the wheel well a little bit so we're updating the transform component of each wheel which is why we needed references to all of those transforms down here okay so what we'll do is just call update wheel pose which is our helper function for each one of our colliders and it's corresponding transform so that's going to look something like this update wheel pose for front driver wheel and front driver transform and I'm going to just duplicate this three more times for each wheel will have front passenger wheel and the front passenger transform rear driver wheel rear driver transform and finally rear passenger wheel and rear passenger transform okay now what is this function actually doing it's actually just taking the pose information from the collider and applying it to the transform so that's going to look something like this we want to get the position so we want to store the position set that equal to our transform position and as well as the rotation so returning I'll call that quad for quaternion is going to be equal to our transform dot rotation that's just initializing the vector thering the quaternion variables right there then what I want to do is get the world pose and the world pose is going to out values to our vector 3 in our quaternion so that's going to look something like this Collider dot get world pose so get world pose outs pause and out for quaternion so if you don't know what out is out is basically just saying hey we're going to modify these parameters for you without you actually having to set it equal to anything in other words we don't have to say position is equal to collider dot get world pose we're actually just returning these values back into themselves so whatever this function does to the position is it going to be consistent and in this variable so hopefully that made sense it's a little bit complicated once we get those values back out we want to update the transform position so transformed up position is equal to pause and then transform dot rotation is equal to quad so let's actually all we need to do for this will go back to unity and play this although nothing is going to happen yet because we haven't applied that component to our vehicle so let's find our vehicle we'll add the car controller we have a lot of values here to fill so let's go ahead and do that so we'll do our wheels first front passenger front passenger bar front driver the rear driver and the rear passenger so that wheels are covered and we want to update the transform so front passenger front driver rear driver and rear passenger ok and note that we already have a mock steer angle and a motor force right there we don't have any errors so we can run and we should be able to drive our car around and note that the wheels actually do turn so you can actually see those wheels turning and most of the functionality for this tutorial is actually already done now if you're interested in seeing how we can follow this with a simple camera controller then you can stay tuned we've done a lot of camera controllers on this channel so I'm not going to do anything too complicated it's going to be one of the easier controllers to create we basically just want to follow this car from a certain defined offset ok so let's create our simple camera controller although simple camera controller is already defined so we'll just call this camera follow controller doesn't really matter what you call it at the end of the day okay so as I said this is going to be a very simple camera controller first we want a public reference to the object that we're following so whatever object we're trying to follow should go here object to follow we want a vector3 offset so where's the offset of the camera in relation to the object that we're following we want a float for follow speed we'll initialize this to ten although we're probably gonna be modifying that value and then something for look speed so how fast are we actually looking at the object that we're trying to follow I don't exactly know what these values need to be but we'll go ahead and just initialize both of those to ten okay now let's draw off the skeleton as we usually do we'll have a function for looking at the target so look at target we'll have a function for moving to the target so move to target and then we'll be doing all of this in fixed update I don't exactly know what the deal is with unity sometimes fixed update works and sometimes late update works I can't give you a specific explanation for why that is I found that fixed update works well in the editor and late update works in the build version so keep that in mind but essentially the difference that you'll find is how much the jitter that you get while following objects so let's say in fixed update all we're doing is looking at the target and moving to the target so how do we look at the target first thing we want to do is define a look Direction so what's the direction that we're actually looking to see the target that's going to be equal to what what we'll call the eye - or I'm sorry the the look at target - the eye the eye is the camera so that look Direction is going to be the object that we're looking at - the eye so object to follow position - the I which is this current component transformed a position then what we want to do is get the rotation as it relates to that look different direction so the way we define that is quaternion I'll call that rotation equals quaternion look rotation the rotation that's going to take the direction that we're looking as a vector 3 and then the up vector which is vector 3 dot after we have the quaternion rotation we can set the transform the cameras rotation according to that the way we want to do that is with a look speed variable which means that we probably should use some sort of lurking function so I'll say the cameras rotation so transformed our rotation is equal to quaternion herb first parameter is our current so transfer that position second parameter is our target which is our OT and then how fast we want to arrive there which is going to be look speed times time to Delta time so that covers looking at our target now we want to cover how to move to our target so we'll create a vector 3 for target position and that's going to be based on the offset to the object that we're following so it'll be equal to object to follow that position Plus and at this point we want to we want the offset parameters to be relative to the transform of the object we're following in other words we don't want to use the global coordinate system we want to use the object that we're looking at coordinate system okay so instead of using vector 3 dot up or vector 3 dot right I'll use transformed up or transformed right so object two followed opposition plus object two followed a forward x offset z + object to follow dot right which is so forward is the z axis right is the x axis so x offset x and then object followed dot up which is the y-axis x offset dot y so now that we have our target position we can smooth our way into that target position however we want I'm going to use lerp so I'll say the camera's transformed that position is equal to vector $3 our current which is transformed on position to our target which is target pause and how fast we want to do it which is follow speed times time that delta time and after that all of the code is done now we just want to go to our camera and put that new camera controller on the camera not car controller so camera follower controller there we go we actually have an error so it looks like I might have missed something here some invalid arguments for lerp so let's take a look at that really quickly and some of you might have actually caught this but I'm putting position in here I need to use rotation and we'll see if that fixes the error for us okay so that fixes that now back to our camera we have the camera follow controller right here we want to drag the car onto the object to follow parameter the offset I don't exactly note that's going to be yet I'll modify that at runtime and same thing with follow and look speed so let's go ahead and run this okay and our camera is going kind of crazy right now probably because of its offset I'm going to move that back of it and then up and let's go ahead and drive around so as you can see we are consistently following from the back side of the car I actually would want to look at to happen a little bit faster so I'm going to up the look speed to about 75 and that looks a little bit better that's going to conclude this tutorial so as you can see we're driving around and we have a pretty decent view of our car as we drive around so thanks for watching guys and I'll see it next week
Info
Channel: Renaissance Coders
Views: 169,332
Rating: undefined out of 5
Keywords: unity 3d, unity, unity3d, unity 3d tutorial, unity3d tutorial, unity tutorial, wheel collider, wheelcollider, drive, easy, simple, beginner, basic, how to, driving, car, vehicle, wheel, controller, camera
Id: j6_SMdWeGFI
Channel Id: undefined
Length: 30min 16sec (1816 seconds)
Published: Sat Jun 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.