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

Upgrading Windows 10 Home to Pro

 So I have been thinking about upgrading my Windows 10 Home Edition to the Pro version, but I always get to the point where it seems that I had to reinstall the entire SO and quit. After some investigating I have done it this way: - following this post  on the microsoft site I use one of the default keys for Windows 10 Pro and went to Settings > Update & Security > Activation > Change the product key; - next, Windows will activate the Pro functionalities and asks to restart; - now you have the Pro version but it's not activated, so you have to buy a Windows Pro Key. I went to UR cdkeys  and bought a key for less then €20; - and with the new key went to Change the product key and activated; - and it's done. Disclaimer : I have nothing to do with UR cdkeys so you can use any site to buy you cd key and your experience may vary from mine.