Car Navigation In Unity
By
Anicia Dcosta, Bharath Bommana, Puja Davande
Logitech
G25 Racing wheel
Logitech G25 racing wheel
device used to drive a car in video racing games. It is tool for navigation. It
includes steering wheel, set of pedals and shift gears. With the help of wheel
and pedals user can manage to drive the simulated car.
Racing Wheel |
The Racing wheel has following components:
1)Steering Wheel
2)A set of pedals
Accelerator
Brake
Clutch
3)A shifter
8 buttons
1 D-pad
A gear stick.
Car Navigation
Tutorial
This tutorial will help you to move
a car (rover) in the virtual space. User
will be able to move around in the Terrain (space) provided. We are using
Logitech G25 racing wheel to move the car. We have created the project in unity
and have connected it to G25 racing wheel for the movement.
Creation of the
Terrain:
Import the standard Terrain tool from unity and with the
help of the tools available we created the below Terrain. This space will be
used as a track for the car movement. Create a directional light to light up
the terrain.
Figure 1: Terrain |
Car:
We downloaded the car model from Unity asset store. Search
for Vehicle SUV in asset store. Download the car and place it in the terrain.
Figure 2: Car Model |
This is special
collider used for grounded vehicles. With the help of Wheel colliders we can
calculate friction and wheel physics. Create an empty game object and name it
as Wheel Collider. Create wheel collider under this empty object and assign it
to each of the wheels of the car. Make
this object (wheel collider) as a child object of the car object.
Figure 3:Wheel Collider |
Car Movement Physics:
We use different methods to make the car moving.
SetupWheelFrictionCurve
():
This method is called in the start method. In
SetupWheelFrictionCurve() we simply create a new WheelFrictionCurve and assign it the values that we think
are appropriate for our car. A WheelFrictionCurve is used by Wheel Colliders to
describe friction properties of the wheel tire.
Figure 4: SetupWheelFrictionCurve |
WheelMovement():
This method is used to take input from the user and apply
this to the wheel movement. We are using the input from the vertical as a
torque to the rear wheels and horizontal input as the steer angle to the front
wheels.
Figure 5: wheelMovement |
CarFlipping():
This method
is called in Update function . Inside that method we check the rotation of the
car. It checks if the rotation is at an angle where the car is not drivable
anymore, and if it is, we add the time since the last frame to the resetTimer variable.
If this
value eventually adds up to exceed the value that we have set for resetTime (5
seconds by default), we call the method FlipCarBack.
If
the rotation of the car is not at a bad angle, we set the timer back to zero
instead.
Figure 6: Code for CarFlipping |
FlipCarBack()
:
In FlipCarBack method we
get the car back on it’s wheels and sets it’s speed to zero, so we can start
driving again from a standstill.
Figure 7:CodeforFlipCarBack |
Wheel Rotation:
This method
calculates the rotation of each wheel from its wheel collider(rpm) which is
applied to the each wheel transform. Below is the code for it.
Figure 8: Code for wheel rotation |
Braking System:
Car can be stopped using the brake pedal in
G25 racing wheel. It uses the braking torque of the wheel collider provided by
unity. Below is the code.
Figure 9: Code for braking |
Gears:
Our
Idea of gears is little basic. We took the Top Speed of the vehicle as 160 and multiplied
it by a factor based on the Joystick button pressed. The factor for first gear
is 0.2, second gear is 0.4, third is 0.6, fourth 0.8 and for the fifth gear it
is 1.0. That means the vehicle can go at its max speed only in the fifth gear.
We used simple if –else if blocks in our code to implement this. We had a hard
time to detect the joystick buttons corresponding to each gear, and we were
able to find joystick buttons for only first two gears.
Figure 10: Input setting for gears |
Figure 11: Gears |
First Person and
Third Person views:
For the First Person view, the camera needs to be set inside
the vehicle and should be facing the path in front of the car. For this we
position the camera carefully inside the vehicle and adjust the rotations
accordingly and make the camera, a child object of the vehicle, so it can move
along with the vehicle.
The third
person view, we set it up mainly to show the wheel rotation. We need to position the camera relative to
the vehicle and adjust the rotations to provide a good side-on view from the
rear of the vehicle. Also make this camera a child object of vehicle, so it
follows the vehicle and provide the third person view.
Figure 12: First person and third person view |
No comments:
Post a Comment