Avançar para o conteúdo principal

Let's make a car in Unity 3D

In this post we will make a simple car in Unity 3D.

The Unity 3D physics engine is used in order to give the car a real behavior.

This are the steps:

[1] - Create a new Project



[2] - Add a terrain

[3] - Import some Assets
The assets to import are the environment elements and the SmoothFollow script.


[4] - Model the terrain
Now lets model the terrain creating some mountains, applying textures, like grass and drawing the path.


[5] - Add a empty game object
This gameObject will be used as a container for the car.
Name this gameObject car.

[6] - Add a cube
Add a cube to the car gameObject. Change the position of the cube for 0,0,0.
Change the position of the gameObject car so it stands inside the terrain.

If you like you can add other cubes so the car looks better. Add the cubes always as a child of the gameObject car.

Remover the cubes box colliders and add a rigidbody and a box collider to the gameObject car.
Now let's change the box collider size so it matches the body of the car.


[7] - Add a cylinder
The cylinder will be used to make the wheels. First we build one then its just copy and paste.
Positioned the cylinder near the body of the car, change the size and rotate it.

It should look something like this.


[8] - Remove the collider and add a wheelcollider
Now that the wheel is in place remove the capsule collider and add a wheelcollider.
This component is responsible for the calculations that will make the car physics seam real.

Last, but not least, make the wheel a child of the car.
If you test the car, executing the scene, it will jump and the camera won't show the car.
First the camera, add the smoothfollow to it and define the target to be the car. The distance should be 2.
As for the car it should have a mass of 1000, change it in the rigidbody.

There it is. Just one wheel though.

[9] - Copy and paste the Wheel
At this point we should copy and past this wheel and position each in the corners of the car.
Each wheel should have a name according to the position.

To make it possible to see the wheels spin we will add a material that looks like a rim.

Next make a material with the texture and add the material to each wheel. It ain't pretty but its effective.

[10] - Add a new C# script
This script will control the behavior of the car.

This script should define an array for the wheels, the power of the motor and the max rotation angle of the front wheels.

Drag the script to the car gameObject and then drag each wheel to the array.

Let's declare some private variables.

And a function to simplify the code that needs to access the wheelcolliders.
Next we need to read the user input and rotate the wheels colliders and the wheels

After that the wheels should spin.

In the end let's add the brakes and the engine.


This project is availbale on GitHub

And a vídeo with this tutorial on YouTube

Comentários

Mensagens populares deste blogue

PacMan 3D

In this post we will be making a simple and classic game, the Pacman, with Unity 3D. Let's create a new a new project

Single Page App with C# WPF/XAML

 In this post we are going to create a single page app. The app will have multiple pages that get rendered in the main window. We will be using Visual Studio, C#, WPF and XAML. Let's start by creating a new project in Visual Studio of this type: Next, in the MainWindow, we define the interface structure. On the left side we place a menu and on the right side a DockPanel with a Frame in it. The Frame is the element that is used to render de pages content. Now let's add the new pages. In this example I will add two pages. Click in the Solution Explorer with the mouse right button, then choose Add and Page. The project looks like this. The app content goes on the recently create pages. Because this is just an example I will just change the background color and add a small text. Page1 Page2 Finally the code. Back to the MainWindow we need to create the click events on the menu items. So, in the MenuItem line add the click event and pick New Event Handler. If that option doesn't