• entries
4
0
• views
666

# Unity Wheel Collider for Motor vehicle Tutorial 2018

745 views

## Objective

The main objective of this post is to give an idea of how to work with wheel collider and physics of Wheel Collider.

Want to Make a Car Racing Game?

Having troubles with Car Physics?

Where to use Wheel Collider?

How to use Wheel Collider?

What are the components of Wheel Collider?

How is Wheel Collider Different than other Colliders?

Have these Questions in Your Mind?

INTRODUCTION

Wheel Collider is a special kind of Collider which is used for vehicles. It has in built collision detection technique and physics of actual Wheel.

It can be used for objects other than wheels (like bumper Boats, bumper cars etc which uses suspension force ), but it is specially designed for vehicles with wheels.

Is there is anything special which is not in other Colliders?

Yes, each and every collider has something special (That’s why Unity created them).

In this Collider, you will get all the components which are used to make vehicle drivable.

Do have any idea about components..?

No problem, I will explain components here.

Before that, it's very important to understand how the Wheel Collider component works internally, to get a car working in Unity

Here, I have explained everything.

This is how you look at your dashing and shiny car.

But, Unity doesn’t have good eyesight. So Unity looks at your car like this.

4 wheels colliders and 1 car collider, that’s it!

Now Let’s Discover inside Wheel Collider.

Wheel doesn’t have any shape it is Raycast based.

Mass & Radius: mass and radius of the wheel. (Easy isn’t it ?)

Below I had given a little introduction about physics of wheel collider.

Every Wheel counts it’s sprung mass. Sprung mass (not weight) is used to apply individual force on each wheel.

Suspension Distance is the distance between max droop and max compression. This suspension is calculated based on rest pose.

Suspension Force(F) = (sprungMass * g ) +( jounce * stiffness) - (damper * jounceSpeed)
 jounce -> offset of the wheel relative to the rest pose. stiffness -> that means physics friction. (setting this to 0 means no friction change this runtime to simulate in various ground material). damper -> damping value applied to the wheel jounceSpeed -> wheel moved with suspension travel direction.

Tire simulation rather than using dynamic or static material slip-based material is used.

First we need to setup Scene for that (Don’t Worry that is Easy part).

PART-1: SCENE SETUP

Step 1: Create a 3D plane Object give it scale of (100, 0, 100).

Step 2: Create an empty Object add Rigidbody 3D. Name it as “Car”

Step 4: Take Mesh Collider and add as a child of Car name it ”CarCollider”.

Step 5: Create Two empty GameObject inside Car name them as “Wheel Meshed ” and “Wheel Collider”.

Step 6: Inside Wheel Meshes add 4 empty GameObject name them as “FL” ,”FR” ,”RL” and “RR”. assign Mesh of Wheel (you will get download link below). Set their Position.

Step 7: Inside Wheel Collider add 4 empty GameObjects name them as “Col_FL” ,”Col_FR”, ”Col_RL” and “Col_RR”. Add wheel collider as their component. Set radius of colliders same as the size of mesh and set their position same as the mesh have.

Yeap, Its Done! actually that was the Difficult part to setup scene.

Now Time for really easy part Scripting.

[System.Serializable]
public class AxleInfo
{
public WheelCollider leftWheelCollider;
public WheelCollider rightWheelCollider;
public GameObject leftWheelMesh;
public GameObject rightWheelMesh;
public bool motor;
public bool steering;
}

In this AxleInfo Class, we are going to store info for a pair of the wheel.

Variable Name Variable Type Description
leftWheelCollider / rightWheelCollider WheelCollider To drive a car using WheelColliders inbuilt physics.
leftWheelMesh / rightWheelMesh GameObject To Visualize rotation and movement of wheels.
motor bool Enable movement of this pair of wheels.
steering bool Enable rotation of this pair of wheels.

Now, Let's go for long drive .

This script is to drive Car.

public class CarDriver : MonoBehaviour
{
public List<AxleInfo> axleInfos;
public float maxMotorTorque;
public float maxSteeringAngle;
public float brakeTorque;
public float decelerationForce;
public void ApplyLocalPositionToVisuals (AxleInfo axleInfo)
{
Vector3 position;
Quaternion rotation;
axleInfo.leftWheelCollider.GetWorldPose (out position, out rotation);
axleInfo.leftWheelMesh.transform.position = position;
axleInfo.leftWheelMesh.transform.rotation = rotation;
axleInfo.rightWheelCollider.GetWorldPose (out position, out rotation);
axleInfo.rightWheelMesh.transform.position = position;
axleInfo.rightWheelMesh.transform.rotation = rotation;
}
void FixedUpdate ()
{
float motor = maxMotorTorque * Input.GetAxis ("Vertical");
float steering = maxSteeringAngle * Input.GetAxis ("Horizontal");
for (int i = 0; i < axleInfos.Count; i++)
{
if (axleInfos [i].steering)
{
Steering (axleInfos [i], steering);
}
if (axleInfos [i].motor)
{
Acceleration (axleInfos [i], motor);
}
if (Input.GetKey (KeyCode.Space))
{
Brake (axleInfos [i]);
}
ApplyLocalPositionToVisuals (axleInfos [i]);
}
}
private void Acceleration (AxleInfo axleInfo, float motor)
{
if (motor != 0f)
{
axleInfo.leftWheelCollider.brakeTorque = 0;
axleInfo.rightWheelCollider.brakeTorque = 0;
axleInfo.leftWheelCollider.motorTorque = motor;
axleInfo.rightWheelCollider.motorTorque = motor;
} else
{
Deceleration (axleInfo);
}
}
private void Deceleration (AxleInfo axleInfo)
{
axleInfo.leftWheelCollider.brakeTorque = decelerationForce;
axleInfo.rightWheelCollider.brakeTorque = decelerationForce;
}
private void Steering (AxleInfo axleInfo, float steering)
{
axleInfo.leftWheelCollider.steerAngle = steering;
axleInfo.rightWheelCollider.steerAngle = steering;
}
private void Brake (AxleInfo axleInfo)
{
axleInfo.leftWheelCollider.brakeTorque = brakeTorque;
axleInfo.rightWheelCollider.brakeTorque = brakeTorque;
}
}

I know, this script was a little bit difficult but I am here to help you.

Variable Name Variable Type Description
axleInfos List List of AxleInfo Which contains a pair of wheels data.
maxMotorTorque float maximum Torque is applicable on Vehicle.
maxSteeringAngle float Maximum angle at which wheel is rotate.
brakeTorque float Torque is applied when a brake is pressed.
decelerationForce float This force is applied to stop the vehicle because of friction.

ApplyLocalPositionToVisuals (AxleInfo axleInfo)

• This Method takes one argument: axleInfo. This method is used to Display rotation and position of WheelMeshes.

FixedUpdate()

• This is Unity callBack used to do certain changes in physics at fixed time.
• Inside that, we will take Input.
• Vertical: W/Up Arrow for Forward & S/Down Arrow for Backward.
• Horizontal: A/Left Arrow for Left Side Rotation. & D/Right Arrow for Right Side Rotation.
• Space: To Stop(brake).
• Assign this Script to our “Car” GameObject.
• Add Size of AxleInfo to 2.
• We have 3 methods to Control Car Acceleration(), Steering() and Brake().

You can relate all the methods listed below. These methods are works like how actually Vehicle we drive in reality Accelerator, Brake(No Clutch here).

Acceleration()

• It is used to make the vehicle move forward and backward.
• If forward or backward buttons are not clicked then need to add Deceleration().
• motorTorque is used to add torque (In forward or backward direction) in a vehicle.
• When the vehicle is moving at that time brakeTorque is needed to change to 0.

Deceleration()

• It is used to stop the vehicle when forward and backward buttons are not pressed.

Steering()

• It is used to turn angle of vehicle.
• steerAngle is used to change an angle of the the vehicle

Brake()

• It is used to stop the vehicle using brakeTorque property.

PART-3: ASSIGNING VALUES

Finally at the end,

This is the last thing to make your car drivable. Enable motor if you want to Apply Force and Suspension on That Wheel.

Enable Steering if you want to give rotation Force on that Wheel. Maximum Motor Torque change as per your car’s Rigidbody mass.

Maximum Steering Angle must be between (30-35). Brake Torque depends on how much Brake Torque you want to apply.

Deceleration Force is must be less than Brake Torque.

### Tip

• Experiment with This Value to make your Car run smoothly.

### Caution

• Brake Torque and Deceleration Force value need to be much higher to stop usually(1e+5 to 1e+8).

There are no comments to display.

## Create an account

Register a new account

• ### Similar Content

• Hi,
please take a look at my new little sparetime - Unity3d - WebGL - Project.
I would be happy to get some feedback.
Greets!

• I'm making a 2d platformer game as a learning experience, and I need a character and some backgrounds.
The idea is a person on the brink of death "running" through his memories. The background is his memories.
This game will make minimal revenue, because it'll be a free mobile game.
I'll give you more details if you respond.
Thanks.

• Search GameDev.net directly from the Unity Editor with the GameDev.net Search plugin!
Customize your search for all content on GameDev.net, only the latest, or the most relevant. Or, narrow your search to specific GameDev.net content:
Blogs Articles Forums News Contractors Projects and more!

• Hello! I'm new to the scene of video game developing and was wondering if anyone here has any experience developing 2D fighters and are up for making a little test demo to see if the idea would catch on to the public? I have no way of paying but I want to put the demo onto Kickstarter and I will pay a good amount if I get a good amount. Please, if you want to contact me for more info, add me on my Discord or Twitter. Thank you, and see you later!
Discord: EnderStaffExe#3193)

• Hello,
We are an American based videogame development team that crafts games utilizing neural networks, handcrafted art, studio quality audio, and immersive storytelling to provide the player with an unforgettable experience. Our current team comprises of a Software Engineer, Designer, Producer, Audio Engineer, and a systems admin. Obviously we are missing an art staff! Now, that being said, we have outsourced our concept art for the game to an experienced artist. We are wanting talent to join us to take it the rest of the way with animation and art. If you are interested in joining the team and feel that you are ready to work along a committed team, send a sample of your work and contact information to HereticRecruitment@HereticArts.com.
Job Specifics:
General:
Unpaid. This is normal. We all have fulltime careers outside of the team, but if there is money to be made upon release, it will be split evenly - no questions asked.  All expenses are paid by Administrative Team (GSuite account). So no worries - We want someone to join, have fun and create with us! Expectations of meeting weekly (via internet) and taking direction from producer.  Open minded
Skill sets:
Experience in designing and creating character art.  Full adobe suite.  Maya experience preferred.  Experience with developing for the Unity Engine.  Willing to work with design team to incorporate their ideas into art.  Knowledge of game design documentation and how it pertains to art creation.  In depth sprite animation knowledge.
×