I see this all the time. Some one posts a piece of code asking how to make Trasform.Translate or whatever work for a car… make it believable. I say BUPKIS. Controlling a vehicle by code is about the most difficult thing to do, and make it look right. Does that mean you shouldn’t, no. It means there is an easier way…
I am going to attempt to go from scratch to a working vehicle in a tutorial in this multi-part tutorial. The best thing is, I am going to do 90% of it with stuff found here in THIS forum. It’s all here, I have done it before. Let’s do it again.
The Basics:
First things first, we are not going to use anything that Unity doesn’t have to start with. We will get to making a car and importing it and doing whatever later.
We start with a nice NEW scene. Into this scene we are going to add the following packages:
Character Controller.unityPackage
Projectors.unityPackage
Scripts.unityPackage
Skyboxes.unityPackage
NOTE: Not a single one of these it totally nessecary, they will be used later on in the more advanced areas. The only thing I usually import is the Scripts, because it contains the SmoothFollow script.
OK with a new scene, lets create a box, name it Ground. set its scale to 500,1,500 and it’s position to 0,0,0. Being the ground, this is of course what we are going to be driving on, so were making it nice and big.
Next, we are going to create a box, and name it Car. Set its scale to 3,1,5 and its position to 0,5,0. This is the building block of the car.
OK, we have 3 pieces in our scene, Car, Ground and Main Camera.
From the Standard Assets folder, we are going to the Scripts folder and the Camera Scripts folder in that. We find a SmoothFollow script in there. Drag that onto the Main Camera in the Hierarchy view.
In the Hierarchy view, click on the Main Camera, and in the Inspector, scroll down to the Smooth Follow (Script). The Target is None (Transform). We need to drag the Car from the Hierarchy view onto the None (Transform) label there.
Now, we are going to add a Rigidbody to the mix. So with the Car selected, goto Component/Physics/Rigidbody.
Scroll down to the Rigidbody in the inspector and change the Mass to 1000;
The next thing we are going to add, is a Directional Light. (GameObject/CreateOther/Directional Light) Rotate it some in the screen so that not everything looks so bland.
Now we can play it, and you will see the camera moving some, and a block, it falls, hits the ground, and it’s still pretty bland.
OK, lets create some wheels. (GameObject/CreateOther/Sphere)
Create one, and we want the Scale to be 1,0.2,1. We will need to rotate it, and move it to a location where a wheel should be. The rotation should be 0,0,90 and the position should be about 1.5, 4.5, 1.8. This is all in how you look at it though.
OK, next, we are going to set this wheel up. First, we need to parent the wheel to the car, and label it… In my case I am going to label the wheel FrontRight. Next, delete the Sphere Collider from the wheel. (Click on the Gear beside the Sphere Collider in the Inpsector and click Remove Component.) Now Add a WheelCollider to the Wheel. (Component/Physics/Wheel Collider)
OK, now we have a basic wheel collider attached to one wheel. NOTICE how the radius of the wheel collider is 0.5. This is because the Scale of the original scale of the wheel is 1. This will be very important in later tutorials.
OK, now if we run it, it is going to drop, hit the ground, but will hobble on the one wheel we set up.
If you are along with me, then we can go to the next step.
Click on the wheel we just set up, duplicate it (Ctrl-D) and move it to the back. (the measurements have changed, now we are in the local measurements, so it is 0.5, -0.5, -0.366) Rename this one, RearRight
Select Both wheels, and duplicate them. Then move them over to the other side. (move them over, then change thier X values to -0.5) Rename them repsectively FrontLeft and RearLeft.
OK, now with everything in place, we run it, it falls to the ground, but has no springs, so it sits there.
So lets build up some suspension on it. This is what is going to make it react like a car.
For each wheel, we need to set 6 values. The Spring Distance (All wheels should be 0.25), the Suspension Spring.Spring (1500 for the front wheels, 1000 for the rear), the Suspension Spring.Damper (All wheels are 2), the Suspension Spring.Target Position (All wheels should be 0.25), the Forward Friction.Stiffness Factor (All wheels should be 0.02) and the Sideways Friction.Stiffness Factor(All wheels should be 0.02)
This means, that a 1000 KG car takes 5000 (or five times the mass) units of force to keep it springy.
Now for a simple piece of code to make it all work:
//CarController1.js
var wheels : Transform[];
var enginePower=150.0;
var power=0.0;
var brake=0.0;
var steer=0.0;
var maxSteer=25.0;
function Start(){
rigidbody.centerOfMass=Vector3(0,-0.5,0.3);
}
function Update () {
power=Input.GetAxis("Vertical") * enginePower * Time.deltaTime * 250.0;
steer=Input.GetAxis("Horizontal") * maxSteer;
brake=Input.GetKey("space") ? rigidbody.mass * 0.1: 0.0;
GetCollider(0).steerAngle=steer;
GetCollider(1).steerAngle=steer;
if(brake > 0.0){
GetCollider(0).brakeTorque=brake;
GetCollider(1).brakeTorque=brake;
GetCollider(2).brakeTorque=brake;
GetCollider(3).brakeTorque=brake;
GetCollider(2).motorTorque=0.0;
GetCollider(3).motorTorque=0.0;
} else {
GetCollider(0).brakeTorque=0;
GetCollider(1).brakeTorque=0;
GetCollider(2).brakeTorque=0;
GetCollider(3).brakeTorque=0;
GetCollider(2).motorTorque=power;
GetCollider(3).motorTorque=power;
}
}
function GetCollider(n : int) : WheelCollider{
return wheels[n].gameObject.GetComponent(WheelCollider);
}
Save the code, drag it to the Car and let it rip.
What you have, the car falls to the ground, allows you to drive it. (if you click on the Car, and click the Gizmos in the top of the Game sceen screen, you can see what it basically sees in physics.)
Two things I want to add here, for your use… One… add some textures so that things aren’t so bland… And you could use the MouseOrbit script, instead of the SmoothFollow. This allows you to see things happening a little better.
I hope this at least gets some of you started. I will be adding onto this later. There is alot more to cars and vehicles than a simple box.