Unity Wheel Collider for Motor vehicle Tutorial 2018

July 6, 2018
protect

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?

No worries. You'll know everything about Wheelcollider after reading this post.

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. You will get to know most of the components here.

To get a car working in Unity it’s useful to understand how the Wheel Collider component works internally. Here we attempt to explain that in just a few minutes.

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

Actual World Car

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

Car as per Unity

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

Now Let’s Discover inside Wheel Collider.

wheel Collider toolbox

Components of unity toolbox

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.

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.

To learn more about physics of wheel collider click here

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 3: Import 3D Car Model inside your Scene (you will get download link below) add as a child of 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.

wheel collider Scene

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

Now Time for really easy part Scripting.

PART-2: SCRIPTING (Check Script Reference Click here)


[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, We are going 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

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Read More>>