Feu Unity 3 D Lesson 2
Feu Unity 3 D Lesson 2
Page 1 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.
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.
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.
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
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”.
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 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.
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.
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
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
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.
Now go ahead and paint. Its fewer, but still not quite.
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:
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
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”.
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.
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.
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.
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.
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.
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
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.
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:
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.
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.
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.
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.