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
[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
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.
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
Enviar um comentário