Adding and Driving a car in Unity using built-in Vehicle Assets

b1

Unity offers many ways to navigate through and explore the 3D environments that you have created. For the first 4 versions of Unity, the First-Person and Third-Person Controllers are quite common objects to use; however, since Unity 5, we also avail of several vehicles as built-in assets that we can use to jazz-up our gameplay. In this tutorial, we will learn how to quickly and simply add, use, drive and follow (using built-in camera)  the car vehicule.

Note: For this tutorial, I have initially created a terrain; however, you can, if you wish, create a box that you rescale on the x- and z-axes (and also add a texture) to simulate a ground.

First, let’s import the vehicle package.

  • Create a new Scene (File | New Scene)
  • Select: Assets | Import Package | Vehicles from the top menu.
  • A new window labeled Import Package will appear.
  • Click on Import to import all assets within.

Once the process is complete, we can check that these have been imported properly by going to the folder Assets | Standard Assets | Vehicles, and checking that it includes both folders Aircraft and Car (we will look at the aircraft asset in the next tutorials).

car1

Folders created for the vehicles

  • Go to the folder Assets | Standard Assets | Vehicles | Car | Prefabs and drag and drop the asset labeled car from its folder onto the scene view, near the First-Person Controller.

car2

Adding the car to the environment

if you already gave a First-Person Controller in the scene, we can temporarily deactivate it so that using the arrow keys on the keyboard only moves the car, and we will also ensure that the car is now the target of (or is followed by) the main camera.

First lets include a new camera to the scene that follows the car:

  • Go to the folder Assets | Standard Assets | Cameras | Prefabs
  • Select the object FreeLookCameraRig and drag and drop it to the scene

car3

Adding the Camera

  • Then set the car as the target for this camera by dragging and dropping the car object to the Target attribute of the camera.

car4

Setting the car as a target of the camera

  • Then, we will deactivate the First-Person Controller since we will not be using it for navigation. To do, so, select the First-Person Controller in the scene and, using the Inspector window, deactivate this object (uncheck the box as illustrated on the next figure).

car5

Deactivating the FPS Controller

  • And last, we will also deactivate the main camera, using the same technique; using the inspector deactivate this object (uncheck the box as illustrated on the next figure).

car6

Deactivating the main camera

Once this is done, you should be able to play the scene (CTRL + P), and see the car moving as you press the arrow keys (i.e., forward and back to accelerate or stop the car).

car7

Driving the Car

If you want to learn more about driving vehicles (e.g., cars or airplanes) or creating realistic 3D environments, Check the Book:

*** Unity 5 from Zero to proficiency (Foundations) ***.

ztp_foundations

Save

Related Articles: