0% found this document useful (0 votes)
15 views

Feu Unity 3 D Lesson 2

Uploaded by

shotokaipr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Feu Unity 3 D Lesson 2

Uploaded by

shotokaipr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Game Programming Using Unity 3D Lesson 2: A Basic Level.

Page 1 of 45

Game Programming Using Unity 3D

Lesson 2: A Basic Level

Ferdinand Joseph Fernandez


Chief Technological Officer, Dreamlords Digital Inc.
Admin and Co-founder, Unity Philippines Users Group
September 2011

This document except code snippets is licensed with


Creative Commons Attribution-NonCommercial 3.0 Unported

All code snippets are licensed under CC0 (public domain)


Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 2 of 45

In this lesson, we'll be making a simple outdoor level that the player can walk through. We'll be
putting various objects that are typical in a video game. We'll put trees, hills, a campfire, and some
music.
Basically you'll go through the things in Unity that are already there. This is so you would know that
you don't need to create source code for these things from scratch anymore.

Adding The Ground


First off, we need to add the ground that the player will walk on.
Start Unity. We'll be making this project from scratch. In the Project Wizard Dialog, click on the Create
New Project tab. You can click on the Browse button to choose the folder where you want the project
to be. Make sure you create a new folder named “Lesson2”, and use that.

Finally, click Create. You'll be greeted with an empty scene except for a camera.
Now go to Terrain > Create Terrain. You should see a large flat platform appear. This is your terrain.

Making Mountains: The Raise Terrain Tool


In the Inspector View, you'll see a new kind of component, the Terrain component.
Look at the row of buttons in your Terrain component. These are tools that will help you spice up your
ground terrain.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 3 of 45

Go ahead and click the leftmost button. This is the Raise/Lower Terrain tool. Now move your mouse
cursor to that flat land in the Scene View. You'll see a blue glow following your mouse. This is your
terrain brush.

Go ahead and left-click. You'll see a mound form up. Just imagine as if you are painting, but instead
of putting in colors, you'll be putting in mountains!
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 4 of 45

Illustration 1: The terrain after a few applications of the Raise Terrain Tool.
Feel free to create mountains in any way you want. If you make a mistake, you can undo it with
Ctrl + Z.
Further down the terrain component, you'll see a selection of brush shapes. Go ahead and try them.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 5 of 45

Let's look at the part labeled “Settings”. Brush size is pretty self-explanatory; it controls the size of
your brush. But what about opacity? Opacity controls the strength of the brush. It controls how high
the terrain becomes when you click.

Try This
Pick the star-shaped brush. Use a very low opacity setting, like 3.
Now brush away like crazy: Hold the left mouse button and brush, instead of just clicking.
You'll see it helps to add a lot of bumpiness to your smooth areas.

Illustration 2: Using the star-shaped brush with an opacity of 3 helps roughen up the flat spots of the terrain, making it look
more realistic.

Creating Cliffs: The Paint Height Tool


Now click on the second button. This is the Paint Height Tool. It lets you set the ground's height at an
exact amount you specify.

First, hold Shift and click on one of your mountains. This is important. This makes Unity
remember the height of that mountain you clicked.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 6 of 45

Now choose the biggest circle-shaped brush (the fourth one) and brush away. You'll see the ground
come up flat.

Smoothing Terrain
The edges of our cliff is too blocky. We'll need to fix that.
Click the third button on the terrain tools. This is the Smooth Height Tool.
Simply click on the edges of our cliff. You'll see how much difference this makes in the picture below:
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 7 of 45

Making Holes: The Lower Terrain Tool


Now what if you want to make pits? Actually you've already encountered the tool to do that. The
Raise Terrain Tool is actually the Raise/Lower Terrain Tool.
Choose the Raise/Lower Terrain Tool. Brush just like you would, but hold Shift while brushing. This
will make depressions on the ground.

Why Can't I Make All These Holes?


You may try using the Lower Terrain Tool and wonder why you can't go lower than what's already
there.
Actually, the flat area that you start with, is the bottommost level of the terrain. You can't go lower than
that. If you want to create depressions further, you'll have to raise everything else in exchange.
Unity lets you raise the whole terrain all at once. You can do this by choosing Terrain > Flatten
Heightmap....

Unfortunately, it flattens your whole terrain as a side-effect. So make sure to do this before you work
on your terrain.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 8 of 45

Adding Texture
Our terrain is looking pretty bare with all that gray color. We'll make use of an image to give better
color to our terrain.
Choose Assets > Import Package > Terrain Assets. You'll see a window showing various files that
you're going to get.

We're basically adding a few files to our project. Unity lets you transfer files between projects by what
they call “Unity Packages”. This package comes default from Unity Technologies itself.
Go ahead and click the Import button at the lower right corner of that window.
Now you'll see the Project View has a new folder called “Standard Assets”. Inside this are a few files
that we'll be using.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 9 of 45

Locate the “Grass (Hill)” image that you see in the picture above. That's the image that we'll overlay
on our terrain.
Click on the fourth button on the terrain tools. This is the Paint Texture tool.

You'll see an “Edit Textures” button there. Go ahead and click it.

Click “Add Texture” from the menu that appears. You'll see the “Add Terrain Texture” window appear.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 10 of 45

Drag the “Grass (Hill)” image to the area that says None (Texture 2D). This is the one to the right of
“Splat”.

Now click on the “Add” button to finalize it.

Now you'll see that image used on the terrain.


Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 11 of 45

Try This
You can change the display size of the image used on the terrain with the “Tile Size X” and
“Tile Size Y” values. You can access that by clicking on “Edit Textures” again and choosing
“Edit Texture...”.
Bigger values mean it gets magnified. Play around with the values until you get something
you want.

Adding more texture


You can add another “texture” the same way. Choose “Cliff (Layered Rock)” this time. Use 50 for the
“Tile Size X” and “Tile Size Y” values.
Now go ahead and paint on your mountains.
You can add as many new “textures” as you want and paint with them.

Adding Trees
Now let's add trees. It works the same way. We specify which tree we will use, then “brush” it on the
terrain.
Click the Place Trees Tool.

Like before, we go to “Edit Trees” and choose “Add Tree”:


Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 12 of 45

You'll be greeted with a window that looks familiar.

This looks almost the same as the “Add Terrain Texture” window. And in fact, you can assign the tree
in the same way we did before (dragging and dropping). But here's a different way to do it. Click on
the small circle next to “None (Game Object)”.
You'll see another window pop up. This window lets you select the
3d objects you have in your project. Right now, we only have a
palm tree. (It came from the Terrain Assets package we imported
earlier.)
Go ahead and double-click on it.
Then finally click “Add” on the “Add Tree” window.
Now you can go painting trees on the terrain!
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 13 of 45

Go ahead and fill your land with trees. If you ever make a mistake and fill trees on a spot you don't
want to, you can erase trees. Hold Ctrl while painting trees to erase them instead of adding
them.

Illustration 3: Hold Ctrl while painting trees to erase them.

Warning
Try not to go too crazy when adding trees. If you add too much, you'll strain your PC in trying
to display all of them.
Unity will try its best to lessen the strain on your PC. But if you feel your PC starting to get choppy or
sluggy, erase some of those trees to compensate.

Illustration 4: Unity actually displays the rest of the trees as flat images to lessen the
strain on the system.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 14 of 45

Wind Swaying
Here's a relatively easy way to add wind to your trees.
Go to your terrain's Place Trees tool. Double-click on the palm tree.

Now input a value to “Bend Factor”. 0.5 is a good number for now.

Now we need a special game object to handle the wind for us. Go to Game Object > Create Other >
Wind Zone.
You'll see in the Inspector its got a few settings. Use the values in the picture below for a good
starting point:

Now let's see that wind blowing. Move you camera somewhere where you get a good view of the
trees. Start your game. You should see the trees sway. If not, you may need to tweak a few more
settings.
Go to the last tool in your terrain. This is the Terrain Settings tool. You'll see a property called
“Billboard Start”. Drag its handle to the far right. Now try again. What was happening was Unity was
showing a picture version of the tree instead of the 3d shape. It does that to conserve computer
resources.

Adding Lights
If your scene is too dark, add a light. Go to Game Object > Create Other > Directional Light.
Directional lights mimic sunlight.
Its doesn't matter where the directional light is. What's important is where its facing. Rotate the light's
game object to change the angle of the light.
You'll be introduced to the other light types later on.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 15 of 45

Adding Grass
Adding grass pretty much works the same. But this time, we need only an image of the grass, not a
3d shape like the tree.
Click on the second to the last button in your terrain tools. This is the Paint Details Tool.

By now you should be familiar with how this works. Click on “Edit Details” and choose “Add Grass
Texture”. The “Add Grass Texture” window will appear.
Locate one of the grass images in your Project View. Drag it to the “Detail Texture” slot. Click “Add” to
finish the process.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 16 of 45

Now paint on the ground to add grass. If you can't see


anything, make sure you are zoomed-in well enough.
You may want to lower the “Target Strength” to
lessen the number of grass blades applied on the
ground when you brush.

You'll notice that the grass doesn't look good when looked
at from the top. The grass is actually just a flat image
propped up in the 3d world. It tries its best to look good,
but on some camera angles it really shows.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 17 of 45

Now in the Inspector View, double-click on your grass texture.

You'll see, in the window that appears, a checkbox labeled


“Billboard”.
When Billboard is checked, the grass will try its best to always
face your vision.
Go ahead and uncheck it. The grass now doesn't bother
trying to always face you. Its really a matter of taste which
you should choose.

Try This
Paint your ground with an image that has the same color as your grass. This makes it look
more believable.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 18 of 45

Adding Rocks
We can litter our terrain with rocks the same way we paint grass on the Information
ground: with the Paint Details Tool. If you want to
have grass
But before that, we need to have the 3d shape, or “3d model”, of our rocks. that's not just a
We'll be importing a custom Unity package to get that. flat image, you can use a
3d model for that. It works
Go to Assets > Import Package > Custom Package.... the same way with adding
rocks; using the Paint
Details tool.

You'll have to choose the file that contains the rock we need. Go to our lesson assets folder. Unity
package filenames end in “.unitypackage”. The file we're looking is named “Rock.unitypackage”. Go
ahead and use it.
Like how we imported the standard Terrain Assets before, you'll see a list of things that will be added
to the project. Click Import to finish adding them.
Now we're ready to add those rocks.
We need to go back to the Terrain component.
Select your terrain game object from the Hierarchy
View. Go again to the Paint Details Tool (second to
the last button). Click “Edit Details”. This time,
choose “Add Detail Mesh”.
This works the same way, but instead of dragging
an image to the slot, you drag the rock 3d model to
it.
Now here's a few things we need to change:
Change the “Healthy Color” to white. Do the same
for “Dry Color”.
Click the “Grass” value in “Render Mode” and
change it to “VertexLit”.
Okay, we're ready. Click Add to finish it.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 19 of 45

Information
What was that Healthy Color and Dry Color anyway?
Healthy Color is a color tinted over your grass or rock. So for example, if for some
reason you want some red grass, instead of having to make a red grass image all over again, just
choose a red color from there.
The nice thing is you can change whatever this color is during run-time using scripting. We'll explore
that later.
Dry Color is an additional color that the grass or rock gets tinted to over time. What happens is the
grass/rock tint goes back and forth between Healthy Color and Dry Color smoothly.

Now go and brush. If you haven't changed any brush settings, you'll see that we're adding too much
rocks in one place. Again, if you're not seeing anything, you're probably not zoomed-in well enough.

Here's what we need to do to fix that.


First, erase the rocks. We do this the same way with erasing grass. Hold Ctrl while brushing to erase
the rocks.
Now, to lessen the rocks being added: find the brush settings in your Paint Details Tool. Move Target
Strength to the left. This gives it a low value. Make sure its a low value, but not zero!
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 20 of 45

Now go ahead and paint. Its fewer, but still not quite.

Let's go back to the brush settings.


Change the Opacity really low. Something like 0.05 is good. Now try it.

That's better. Tweak the settings more until you get something that you like. As always, you can erase
if things don't come out the way you want it.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 21 of 45

Information
What's the difference between Target Strength and Opacity?
Opacity limits the amount of rocks that you can apply on one brushing (when you click
once).
Target Strength instead sets the maximum amount of rocks that you can have at a given place, no
matter how many times you brush over it.

Adding A Campfire
We'll be making a little fire, but first we need the campfire 3d model for this.
Same thing with the rock, we'll add the 3d model to our project. But instead of a Unity package, we'll
add the 3d model and its texture directly.
Open your Explorer (Windows) or Finder (Mac). Go to the lesson assets folder. Find the folder that
says “Campfire”. You'll see it has a few files inside. Those are what we need. To get them all inside,
drag the folder to the Project View in Unity's window.

Wait for it to finish importing. Once done, you'll see your Project View now has a new folder named
“Campfire”. All the files should be inside.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 22 of 45

Additionally, you'll see a new folder inside called “Materials”. Unity automatically creates this for the
necessary so-called “materials” that the 3d model needs.
You should see the campfire file, the one with a blue box as its icon. This is the 3d model. Click on it.

You'll see in your Inspector View a preview of what the 3d model looks like. Unfortunately, the texture,
or the colors, of the 3d model hasn't been assigned. Its just a plain white color. We'll fix this. You'll see
a file named “Campfire_texture”. That's an image file used to give color and detail to the campfire 3d
model.
You'll see in the “Materials” folder, there's one file. The name for this is automatically generated. It
should be something like in the next picture.
This is the “material” that the campfire uses. We need to assign the campfire image file to this.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 23 of 45

Find the Campfire_texture file and drag it into the texture slot as indicated in this picture:
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 24 of 45

If you check your 3d model again, it should now have the texture applied.

Now finally we'll add the campfire to our scene. Go and drag the campfire file from the Project View
into your Scene View. Position it anywhere you want.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 25 of 45

Adding Fire
What's a campfire without a fire? Let's put a fire effect on our campfire 3d model. We'll be using the
technique called “particle effects” to achieve this.
The idea is we take a flat image of fire, like this one:

...and we let Unity display many copies of that in our scene and animate it over time:

Illustration 5: Each square displayed here is a copy of the fire image


shown before.

That's the idea with particle effects. Each image is a “particle” that has properties, like how fast it
moves, how long until it disappears, etc.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 26 of 45

We'll use a default particle emitter as our template. Choose GameObject > Create Other > Particle
System. It'll make a game object that emits white balls as particles. Move it to your campfire 3d
model.

We'll need the fire image for our fire. You'll find one in our lesson assets folder. Use Explorer
(Windows) or Finder (Mac) to find the “fire4.psd” file. Drag it to your Project View.
Now we'll be making our own material file for the fire. We need a material file since this is what the
particle system expects. You'll see a Create button in your Project View, just like in the picture to the
left:

Click it and you'll see a menu of things that you can create. Choose
“Material” like in the picture to the right.
You'll then have a new material file that you can rename. Name it
Fire.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 27 of 45

Now drag the fire4 image file to the texture slot for your newly created Fire material.

The preview will now show the fire image being used.
Let's leave that for a second and take a look at our particle system. In your Hierarchy View, find the
“Particle System” and click on it.
Take a look at the Inspector. There's quite a lot of values, but let's break it down in pieces.
First of all, we have three components working hand-in-hand to make this particle system work.
1. Ellipsoid Particle Emitter: this controls the initial properties of the created particles, like how
many particles are created, the lifetime of each particle, the velocity of each particle, how much
space the particles take, etc.
2. Particle Animator: this concerns the properties of each particle that changes over time, such
as color change, or velocity change (acceleration)
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 28 of 45

3. Particle Renderer: this manages how the particle should look


Its this Particle Renderer that we'll concern ourselves with first. Find the Materials property of the
Particle Renderer. You'll need to click the small triangle/arrowhead to see the list of materials that this
particle system uses. Drag the Fire material that we created into the Material slot of the Particle
Renderer. Replace the default one it uses there, so that it won't show white balls anymore.

You'll see the results show up in our Scene View.

Unfortunately, it doesn't look right yet. First off, we need to get rid of that black background that the
fire image has. There's a quick way to do this.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 29 of 45

Using Shaders
Find and select your Fire material.
In the Inspector View, you'll see a label called “Shader”.

Click it and from the menu that appears, select


Particles, then Additive.
You'll see in the preview area that the black
background problem is fixed.
What's more, it makes the fire image look
brighter. What happens is Additive makes the
objects that are overlayed on top of one another
brighter and brighter the more they pile up.
This is one thing that shaders do. It changes
how pictures are shown in your game. We'll
learn more about shaders later on.
If you look at your Scene View, you can see the
fire image displayed properly. So far, so good.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 30 of 45

Particle Size
Next, let's fix the size of our particles. Its too small. Select your particle system game object.
Locate the Ellipsoid Particle Emitter component. In here we'll find the property to change the size of
our particles.
Change Min Size and Max Size to 0.5.

Emission Area Size


Well, now its bigger, but the problem is the fire is spread
too large. We need it to make particles inside the
campfire only.
Actually, our particles are contained inside an ellipsoid
shape. Its this shape that we need to modify.
Find the Ellipsoid property. Click the triangle/arrowhead
beside it to expand the properties inside. The X, Y, and Z
values denote how wide, tall, and long the ellipsoid will
be. Change all of them to 0.3.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 31 of 45

Now that's looking better. We're getting somewhere here.

Particle Velocity
Next, lets make the fire particles rise. We do this by giving the particles a velocity.
You'll find a World Velocity property in there. Expand it like
before. Give the Y velocity a value of 1.

Woah! Looks like someone put too much gasoline in there.


Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 32 of 45

Particle Lifetime
The problem now is the fact that the particles take too long before they disappear. We need to lower
the particle lifetime.
In Unity, the particle emitter's “Energy” property controls the lifetime. We can specify a minimum and
maximum value and the particle system chooses a random value between them for each particle.
Change Min Energy to 0.5 and Max Energy to 1.

Now we finally got a good fire particle effect!


Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 33 of 45

Adding Smoke
Let's build upon this more. We'll add a black smoke particle effect on top of our fire.

For the smoke, let's reuse the fire4 image that we have. The only thing we need to do is duplicate our
Fire material and use that.

Duplicating Assets
To duplicate the Fire material, locate it in your Project View. Select it. Now press Ctr + D. You'll see a
duplicate entitled “Fire 1”.
Another way to duplicate is to drag the Fire material to the space just below it. Make sure to hold Alt
first, and only then stop dragging. Your duplicate will arrive at the folder you dropped your mouse to.

Renaming Assets
Let's change the duplicate's name from “Fire 1” to “Smoke”. To do this, select it and press F2
(Windows), or if you're in a Mac, press enter. Then you'll be able to rename it. Press enter afterwards
to confirm.

Using The Multiply Shader


We're just reusing our fire image, but we need to make it look like smoke. How do we do that? We'll
change our new Smoke material to use a shader that darkens the image.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 34 of 45

First of all, select your fire4 image from the Project View. In the Inspector you'll see the Import
Settings for that image. Check “Generate Alpha from Grayscale” so we can have transparency.
Select your Smoke material. Change the shader used to Particles, Multiply. Multiply makes the
objects that are overlayed on top of one another darker and darker the more they pile up.

Making The Smoke Particle System


We'll just duplicate our existing fire particle system and use that as our
template. Select the “Particle System” and press Ctrl + D to duplicate it.
We got two game objects both called Particle System. Now would be a good
idea to name one “Fire Particle System” and the other, “Smoke Particle
System”.
Like in files, just press F2 (in Windows) to rename a game object, or press
enter if you're on a Mac.
Change the Smoke Particle System to use our Smoke material. Like before,
just drag and drop it.
Because of the Multiply shader, we got it to look dark, but its not enough. We
need to make it black or gray, and not reddish as shown in the picture.
We need to give it a color change over time. This is the domain of the Particle Animator.

Giving Color Change


Locate the Particle Animator component. You'll see white bars that denote colors. These specify the
particle's color over the course of its lifetime.
Color Animation[0] is the particle's color at the start, Color
Animation[4] is its color at then end, and the others are its
colors in the middle.

Now change the colors for 2, 3, and 4 to gray, like in the next
picture:
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 35 of 45

Click on each bar to change its color.


You'll see the particles get a better, darker look.
If you want the smoke to rise longer, increase the lifetime. Go to the Ellipsoid Particle Emitter and set
the Min Energy and Max Energy to larger values, like 1.0 or 2.0.

That's it for particle systems!


Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 36 of 45

Adding The Default First Person Character Controller


Wouldn't it be nice to be able to walk on our created scene? Programming is not our focus in this
lesson, so we'll just use a premade solution to do that. We'll learn about programming this on our own
later on.
For now, we'll import a package that lets us walk on our scene in first person.
Locate your lesson assets folder. Import the custom package called “FpsController.unitypackage”.
Remember importing is in Assets > Import Package > Custom Package....
In our Project View, we'll have a new folder named FpsController. Look inside and we have a file
named Player. Drag this to your scene in the Scene View.

In your Hierarchy View, we need to delete the default camera to remove conflicts with our First
Person Controller. Select “Main Camera” and press Delete.
Now start the game. You should be able to move through your scene with WASD keys in the
keyboard and rotate your facing with the mouse.

Adding A Skybox
Let's add a sky image next. Unity uses what is called a “skybox”.
What happens is an image of the sky is displayed on the insides
of a cube, and put in the background of the 3d world.
Unity provides some default skyboxes for you to use. Go to
Assets > Import Package > Skyboxes. You'll get a selection of
quite a few skyboxes.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 37 of 45

To set which skybox is used, we'll go to Render Settings. Choose Edit > Render Settings.
Just drag any skybox you want to the “Skybox Material” in the Render Settings. In this lesson, I'll be
using the StarryNight skybox.

You'll see the changes in your Scene View. If not, you can preview it by clicking on the button that lets
you preview scene render properties:
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 38 of 45

Adding Fog
While you're at the Render Settings, we'll take a look at another setting for our scene.
Check Fog in the Render Settings. Use black for the Fog Color, and adjust the Fog Density to 0.005.

You'll see the difference in this picture:


Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 39 of 45

Try This
Fog color looks really good if it matches the color of the sky that you used.

Fog Mode
Fog Mode dictates how smoothly your scene transitions into the fog color over distance.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 40 of 45

Adding Sound
Let's make our campfire even more realistic with a campfire sound.
First we'll add the campfire sound to our project. Locate the file named “17298__luffy__luffy-
fire3.wav” and drag it to your Project View.
Create a new empty game object. Position it to be in our campfire game object. We're going to add
the sound there. Go to Component > Audio > Audio Source. The Audio Source component makes
your game object act like a sound speaker.
Drag the sound we imported earlier to the Audio Source's Audio Clip property, as indicated in this
picture:

Check the Loop property so it doesn't just play once.


Now start your game. When you walk near the campfire, you should hear the campfire sound. The
farther you walk away from it, the weaker the sound gets.

Adding Music
Music works similarly. First we'll import our music file. In the lesson assets folder, there's a folder
inside named Music. Go inside and choose any music file you want. Drag it to the Project View in the
Unity window. Wait for it to finish importing.
Select it and look at the Inspector.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 41 of 45

We don't want the music to weaken if we walk away from its Audio Source, so uncheck 3D Sound.
Since music is a large file, its also recommended to set the “Load type” to “Stream from disc”, to
lessen the initial loading time.

Same as before, make an empty game object and add the Audio Source component to it. Drag the
music file to that Audio Source's Audio Clip property.
This time it says the audio is a 2d sound. In this manner, it doesn't matter what position the Audio
Source has in the 3d world. The game will always play the music in a consistent volume wherever it
is.

Adding Lens Flare


The last feature we'll add to our game is what they call lens flares.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 42 of 45

Unity provides quite a few default lens flares that we can use. Go to Assets >
Import Package > Light Flares. You'll end up with about three lens flares in the
Standard Assets folder that you can use.
Before we can use any of these, we'll have to put it in a game object first. So
make a new empty game object (Ctrl + Shift + N). Then we'll add a Lens Flare
component to it. Choose Component > Rendering > Lens Flare.
Now drag one of the lens flare files from the Project View into your Lens Flare component's
corresponding slot.

Usually lens flares come from a light source, so it makes sense to put the lens flare on a light. You
can put lens flares on lights instead for that matter. Just drag it from the Project to the corresponding
slot in your light's Light component.

Why bother having a separate Lens Flare component then? With the Lens Flare component, you can
put your lens flare on the background, as if attached to your skybox.
You can use this for situations like a faraway light source. Like the sun or moon.
To do this, look at your Lens Flare component, then check Directional.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 43 of 45

First thing you'll see is the lens flare will disappear from your game object. This is expected. Now
rotate your game object in the local x-axis (the red arc). Rotating your game object will dictate where
the lens flare is angled at. Its as if you are changing the position of the sun to be sunset or sunrise
depending on the angle.

Publishing Your Game As An .EXE File


We've made a fairly simple self-sufficient scene, but how about letting your friends see it? You can
compile your game into an .EXE file by publishing it.
Go to File > Build Settings... or press Ctrl + Shift + B.

The window that appears will manage the building process.


We need to specify which scenes get included in the build, so click on “Add Current” to have our
current campfire scene included.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 44 of 45

Make sure “PC and Mac Standalone” is selected. Make sure “Target Platform” is set to “Windows”.
Now click “Build”. You need to specify a name for your .EXE file, then confirm.
Wait a while for Unity to compile and pack things. Then you'll see a folder open up where your new
.EXE file is.
Together with this is a folder named after your .EXE file with “_Data” appended to its name. This
folder contains all the necessary resources needed to run your game. Make sure you include it when
you distribute your program.

Publishing Your Game To The Web Browser


You can make have your game running inside the web browser. The process is similar to publishing
as an .EXE file.
Go to File > Build Settings.... Select “Web Player”. Now click on “Switch Platform”. A progress bar will
show up. This is Unity reassessing some resources to make sure they work in the web browser
environment.
Now go ahead and click “Build”. You'll need to specify a folder where the files will be created. Create
a new folder if you have to.
When its done compiling, you'll be presented with a window of the files made. Usually it'll be inside a
folder named “WebPlayer”. Inside is your unity game ending in the “.unity3d” file format, and an
.HTML file that makes use of it.
Double click the .HTML file to view it on your browser.
Game Programming Using Unity 3D Lesson 2: A Basic Level. Page 45 of 45

In Conclusion...
You've gone through the various graphical capabilities of Unity, from the terrain, to importing 3d
models, to particle effects.
Now you know how to create a simple outdoor level, complete with trees, rocks, sky, and even wind!
The next lesson starts programming. You'll learn how to alter the behavior of your game objects with
custom-made components.

You might also like