Simple Car Controller in Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we are going to build a simple arcade style car controller we will learn how the wheel Collider component works how to set its parameters and build a camera follow script let's begin first create a new unity project we will be used in currency 2000 nineteen point two for this let's also make it a lightweight render pipeline project let's remove example assets from the scene first now we will make our ground object so at a plane make it a little bigger and call it ground for our car we will use an arcade free racing car a free package from the unity asset store find the prefabs folder in the package then get any car and place it on our ground let's reset its transform position as well as we can see the entire car is in magenta that indicates the problem shaders to fix it we have to upgrade shaders to the lightweight render pipeline we can do it by clicking edit render pipeline lightweight render pipeline click upgrade project materials and then click proceed now the materials are rendered properly find our car in the scene break the prefab connection and change its name for the wheel Collider components to work the current object has to have a specific hierarchy let's see what it should look like first we will add an empty game object called visual in which we will keep all of our mesh renderer components next add a wheel game object and move all of the wheel meshes there in the wheel game object we will have two children one for the wheel meshes and one for the wheel colliders let's add the first one call it wheel transforms and move the wheel meshes under it now add a rigidbody component on to the root game object will colliders are driven by the physics engine so rigid body is a necessary component next add a box Collider and try to adjust its size and sensor so it covers the car fairly close now create an empty game object - how they will colliders let's name the objects properly now we will prepare the first wheel Collider at a component position it's roughly in the same position as the wheel mesh and tweak the radius value on the components to match the mesh now we can duplicate our wheel Collider game object and position it in place of the other wheels let's also name them correctly now we can move to writing the car script that will drive the wheel colliders so let's go to scripts folder and create a new C sharp script called car controller open that script in code editor all of the steering methods will use the physics engine so we have to use the fixed object method we will first get the user input then we will add first to our wheels then handle the wheel rotations and lastly update the wheel visuals let's declare necessary variables for our gets input method first we will need two floats to store our actual input values let's also declare constants drinks for axis names those names are set in the unity input settings in the method itself we will assign the values from input.getaxis methods to our horizontal and vertical inputs which will take the axis name as the parameter let's also add a braking input when pressing the space bar so at a boolean colt is breaking and set its value equal to space bar being pressed now the handle motor method which will drive the wheel colliders for that we will need references to all of our wheel collider components we will apply force just to the front wheels we will do it by setting the motor force value in the wheel Collider component so let's assign it to our input multiplied by a constant let's call that constant motor force make it visible in the inspector let's also add a serializable field for the brake force it will be positive only when the spacebar is pressed so at a temporary brake force value that will be equal to the brake force assigned from the inspector only when the space bar is being pressed otherwise it will be equal zero if the is braking boolean is true we will apply the braking force it is done similarly to applying motor force but instead of changing the motor torque we will change the brake turk let's assign the current braking force to the brake torque of all the wheel colliders now let's handle the car steering generate a new method and place it in the proper place in the script according to the execution order first we need to declare a variable to store our current steering angle and the serializable variable to set the max steering angle similar to braking and motor torques the steering is driven by changing the variable in the wheel Collider component in this case we change the steering angle variable we need to do it only for the front wheels lastly let's implement the update wheels method which will update the wheel visuals we will use a method that will update a single wheel so you don't have to multiply our code for every single wheel as the parameters we will take a wheel Collider and a will transform we need to assign wheel transforms from the inspector so let's add serializable fields for transforms similar to you colliders now we can pass the we'll transform as a parameter to update the single wheel method to get the position and rotation that we want to apply to the wheelchair we can use the get world pose method in the wheel collider class this method will take vector 3 and quaternion as out parameters and send them based on the changes a wheel Collider has made in this frame now we can use those values and assign them to will transform let's do it for every single wheel so we use the method four times in the update wheels method now let's go back to unity let's add a car control component to the car game object by trial-and-error I came up with the values for motor force 1000 brake force 3000 and steering angle of 30 next add all the necessary inspector fields for the wheel colliders and transforms next position the scene view behind the car and align the camera to the view hit play and let's see what will happen okay move the camera a little bit further now hit play again and our car falls down let's fix that we have to move the ground game object below the car so it can actually stop somewhere so let's reset its transform and scale it again now hit play and see our car bouncing around that's because the mass on the rigidbody component is set to 1 so 1 kilogram car is falling on suspension made for a 1 tonne car let's fix that and set the mass to 1500 and hit play again we have one last problem with our car setup right now even though we press the input button the car sits still we place the Box Collider on the car wrong let's fix that as well the Box Collider has to cover only the car body and not the wheels otherwise the wheel transform will not be able to touch the ground let's get play again and we have our first drivable version that flips when we steer a little bit too much to change that we have to go back to the wheel Collider component this is the place where all of the magic happens we can set up different car behaviors the explanation of those values is a little bit beyond the scope of this video but I strongly encourage you to read the documentation and play around with those values to fix our flippin car let's make the suspension 2 times stronger and hit play again unfortunately our car is still flipping that's because we have to apply suspension changes to all of the wheel colliders let's just copy the component values and apply them to other wheels and hit play again now our car is sticking to the ground oh where did it go there it is it's also sliding nicely when we stood height speed let's hope to the last part of the camera follow script find the camera in the scene view remove the attach script if there is one and add a new script called camera follow open that script in the fixed update declared two methods handle translation and handler rotation for translation we will need a vector three that will be an offset from the camera to the car a reference to the car transform and they speed with which the camera will follow the car now to get a word position from the local position we can use the transform point method that will convert the local position of the transform that this method was called on towards position in our case it is the target saw the car now it can interpolate between the current position and say target position by translation speed it is not the proper use of the lab methods we should use these smooth damped methods but it will work too it is my mistake doing it with the lab methods now for the handle rotation first we need to declare a rotation speed in the method we will find the direction from our position to the target position and converts it to quaternion using the luke rotation method with the y axis as the up direction then we will learn from our current rotation to the target rotation by rotation speed same as with translation it is better to use this smooth dump method and this is it for this script now go back to unity and fill the inspector fields you can use the same values as me or set them to your liking there is one more bug that I found here should be a minus sign and not an equal sign let's save and go back to unity now we can see that the camera is following the car pretty smoothly there are two things I would like to change first fix the camera offset a little bit also we can't really see how fast we are moving so let's add some environment to the scene I will just add some simple pillars now hit play and see the car in its full glory we can accelerate turn break and even bump into things and that's it guys you can find the whole project on github the link is in the description below I hope you enjoyed it and please like and subscribe if you like my content there are new videos every Monday also leave a comment I'm happy to answer all of your questions take care
Info
Channel: GameDevChef
Views: 144,344
Rating: undefined out of 5
Keywords: Unity, Car, Car Controller, Unity3D, Tutorial, Game, Development, Game Development, How to in Unity, Gamedev, GameDevChef
Id: Z4HA8zJhGEk
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Tue Jun 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.