CarTutorialSection_2.pdf

(2669 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
“The Car Tutorial” Part 2
Creating a Racing Game
for Unity
873148039.049.png 873148039.060.png 873148039.070.png 873148039.081.png 873148039.001.png 873148039.002.png 873148039.003.png 873148039.004.png 873148039.005.png 873148039.006.png 873148039.007.png 873148039.008.png 873148039.009.png 873148039.010.png 873148039.011.png 873148039.012.png 873148039.013.png 873148039.014.png
unity
“The Car Tutorial”― Creating a Racing Game for Unity, Part 2
Part 2: Tweaking the Car
3
Suspension damper
6
Center of Mass
3
Drag Multiplier
6
Suspension
5
Speed, turning and gears
8
Suspension range
6
Exporting the car as a package
9
Suspension spring
6
873148039.015.png 873148039.016.png 873148039.017.png 873148039.018.png 873148039.019.png 873148039.020.png 873148039.021.png 873148039.022.png 873148039.023.png 873148039.024.png 873148039.025.png 873148039.026.png 873148039.027.png 873148039.028.png 873148039.029.png
 
unity
“The Car Tutorial”― Creating a Racing Game for Unity, Part 2
Part 2: Tweaking the Car
If you’ve followed along part 1 of this tutorial and assembled the car, you are already at a point
where the car is pretty awesome. But if you have driven it around a bit, you will probably have
noticed that there is after all room for some improvement.
This is where tweaking enters the picture. In game development tweaking is a crucial part of making
your game fun, challenging, awesome or whatever goal you have for your specifi c game. The idea is
that when you have setup the nuts and bolts that makes your game run, there might be something
that doesn’t feel quite right - maybe you want the car handling to be a bit different, maybe you want
the top speed to be slightly different, or maybe you want to change the scene lights.
A major strength of Unity is it’s tweakability - as you have seen, all the public variables in your scripts
are shown in the inspector, so you can change values without going into the code. And even more
powerful: When you’ve made a change you just hit play, and you will instantly see the result of that
change. You never have to wait for the scene to be rebuilt or for a major recompile of the code.
Center of Mass
Now the most obvious thing that needs to be tweaked is probably that the car can very easily be
fl ipped around when turning (if you haven’t tried yet, then play again and speed up a bit and
then turn from side to side while speeding - the car will will fl ip over pretty easily). The reason for
this fl ipping is that we haven’t yet defi ned the car’s center of mass. All the forces that are applied
to the Rigidbody of the car, are all applied at the Rigidbody’s center of mass. The center of mass
of the Rigidbody will be calculated by Unity according to the Colliders attached to the Rigidbody,
either on the same GameObject or on child objects. Since the center of mass of a car is typically not
3
873148039.030.png 873148039.031.png 873148039.032.png 873148039.033.png 873148039.034.png 873148039.035.png 873148039.036.png 873148039.037.png 873148039.038.png 873148039.039.png 873148039.040.png 873148039.041.png 873148039.042.png 873148039.043.png 873148039.044.png 873148039.045.png
 
unity
“The Car Tutorial”― Creating a Racing Game for Unity, Part 2
the center of the car (and probably not the center of mass that Unity calculates), we want to set the
center of mass ourselves.
The position of the center of mass for a car de-
pends on the placement of the engine and other
factors, and it can vary a lot from one car model
to another. For the car in this tutorial project the
center of mass could be a little behind the engine,
slightly above fl oor height of the car. See it’s posi-
tion in this image:
Create a new GameObject and drag it to the
Car game object.
Rename it to
CenterOfMass
Reset the
CenterOfMass’s Transform (click the little cog wheel to the right of the Transform in
the Inspector and select ‘ Reset ’. This will give it the same position as its parent, the car.
Adjust its position to somewhere you like. Either do it by dragging, or type in the position. A
good position for this car’s center of mass could be (0, 0.1, 0.65).
In the Inspector assign the
CenterOfMass to the slot for it in the Car script Component.
In general, it is a bad idea to have the center of mass be positioned to either side of the center in
the x-axis, because this will make the steering behave oddly, and thus we have also set the x vari-
able of the position to 0.
You can also change the Camera’s target to be the CenterOfMass Game Object instead of the car
4
873148039.046.png 873148039.047.png 873148039.048.png 873148039.050.png 873148039.051.png 873148039.052.png 873148039.053.png 873148039.054.png 873148039.055.png 873148039.056.png 873148039.057.png 873148039.058.png 873148039.059.png 873148039.061.png 873148039.062.png
 
unity
“The Car Tutorial”― Creating a Racing Game for Unity, Part 2
itself. This will give a slightly different feeling - play around with it and decide which setting you
like the most.
Suspension
Another factor that can heavily change the behavior of the car is the properties of its suspension.
The job of a car suspension is to maximize the friction between the tires and the road surface.
When you are driving the car over a bump, all of the wheel’s vertical energy gets transfered to the
frame. If we did not have an intervening structure, this could easily result in the wheel loosing con-
tact with the road completely, and afterwards slamming down into the road because of the force
of gravity. The suspension is that intervening structure.
We have three different variables to tweak from the Inspector - the range, the damper and the
spring. All are part of the WheelCollider class that we use on the car’s wheels.
To the left we see the car with the standard settings, and to the right we see it with a much larger
suspension range. Combined with the spring and damper properties, you can make it behave like
everything from a formula one car to a huge monster truck. Of course the graphics need to match
the settings to make it believable though!
5
873148039.063.png 873148039.064.png 873148039.065.png 873148039.066.png 873148039.067.png 873148039.068.png 873148039.069.png 873148039.071.png 873148039.072.png 873148039.073.png 873148039.074.png 873148039.075.png 873148039.076.png 873148039.077.png 873148039.078.png 873148039.079.png 873148039.080.png
 
Zgłoś jeśli naruszono regulamin