In the previous tutorial, you finished making the player character. This character can move around, but it currently doesn’t have anywhere to explore. Now you’re ready to add an environment to your Scene, light this correctly, and add a Navigation Mesh to help the enemies that will eventually roam the halls of your haunted house!
In the previous tutorial, you finished making the player character. This character can move around, but it currently doesn’t have anywhere to explore. Now you’re ready to add an environment to your Scene, light this correctly, and add a Navigation Mesh to help the enemies that will eventually roam the halls of your haunted house!
Constructing environments in Unity is a process that can vary wildly depending on the type of game you are making. Generally this involves instantiating multiple models by dragging them from the Project window into the Hierarchy window. The environment usually needs a physical presence in the scene, so Colliders are then added to each of the models. Often these pieces are Prefabs, so you don’t have to add new Colliders each time.
Creating game environments can be a very labor-intensive process, and you’ve already practiced these processes when you set up the player character. We’ve already created the environment for this game to save you some repetitive work — all you need to do is instantiate it:
1. In the Project window, go to Assets > Scenes folder and double click MainScene to load it.
2. Navigate to Assets > Prefabs and select the Level Prefab.
3. Drag the Level Prefab from the Project window into the Hierarchy to instantiate it.
The Level Prefab is made of many models, including walls, floors, doors, furniture and decorations. If you’d like to see how it was put together, simply expand its hierarchy in the Hierarchy window.
Now that you have an environment in the Scene, you need to position JohnLemon so that the character begins in the correct place:
1. In the Hierarchy window, select the JohnLemon GameObject.
2. In the Inspector, set the Transform component’s Position property to (-9.8, 0, -3.2).
3. To go File > Save and save your changes to the Scene.
4. Enter Play Mode to see your changes. The camera isn’t quite where it needs to be, but there’s now a whole level for JohnLemon to explore and escape.
This environment is great, but it looks a little flat. The next step is to adjust the lighting for the Scene.
Lighting has a big impact on atmosphere — changing the lighting in your game is the first step toward creating a really spooky environment for players to explore.
Lights in Unity are simply GameObjects with a Light Component added. The Light Component contains settings which allow you to change the color and intensity of a light, as well as the ‘type’ of light and various more complex controls for shadows and Lightmapping.
Every new Scene in Unity comes with Directional Light, one of the two GameObjects created as default. Your Scene now also has a series of extra lights added by the Level Prefab.
Let’s start with Directional Light.
The Directional Light in your Scene is set to the Directional Type. Directional Type lights mimic very distant light objects, such as the Sun. They can be placed anywhere in the scene, and rotated to change the angle at which the light affects the scene.
Let’s alter the Directional Light to feel more like moonlight and create some interesting shadows:
1. In the Hierarchy window, select the Directional Light GameObject.
2. You’re going to change some of the properties of the Light component, starting with the Color, but first you need to understand how colors work.
In the Inspector, click on the Color property. Clicking on any Color property in Unity will open the Color picker window:
3. The Color picker window gives you a number of different options to set the values for a color.
Each color is made from four channels:
The amount of each of these channels that makes up a color can be represented in different ways. You can choose the representation using the drop-down menu just below the color ring.
Check that the drop-down is set to RGB 0-255. This representation means that each channel is represented by an integer between 0 and 255.
4. Set the Color property of the Light Component to (225, 240, 250). This will create a very light blue light, rather than yellow.
5. In the Light component, increase the Intensity property to 2.
6. Now you can improve the quality of the spooky shadows in the haunted house. In the Light component, find the Realtime Shadows property. Change its Resolution property from Use Quality Settings to Very High Resolution.
7. Set the Realtime Shadows Bias and Normal Bias properties to 0.01.
8. In the Transform Component, set the Rotation property to (30, 20, 0). This will rotate the light so it shines through the windows of the house.
9. Save your Scene.
The Scene is already looking spookier!
There are two kinds of lighting in Unity which help you simulate real-world light behaviour: Direct and Indirect. Direct lighting is from a specific light source, such as the sun (the Directional Light in your Scene). Indirect lighting is the additional lighting that occurs when direct light bounces off surfaces.
The same Scene: With no lighting (Left), with ‘direct light’ only (Center) & with indirect ‘global illumination’ (Right). Note how colors are transferred as light ‘bounces’ between surfaces, giving a much more realistic result.
This effect is most commonly created in Unity using the Global Illumination Lightmapping feature. Lightmapping simulates the bouncing of light within the Scene and writes (or ‘bakes’) it to an Asset stored within the Project. This takes time, but can result in really realistic-looking scenes.
To save some time you can use an approximation to create a similar effect:
1. Go to the Window menu and select Rendering > Lighting Settings. Click and drag on the name tag, and dock it next to Inspector window.
The Lighting Settings window is the main control point for Unity’s Global Illumination features for the currently loaded scene. It also contains the settings for Environment Lighting, Reflections and Fog
2. In the Realtime Lighting section, disable the Realtime Global Illumination checkbox. In the Mixed Lighting section, disable the Baked Global Illumination checkbox.
3. In the Environment section, use the circle select button to the far right of the Skybox Material property to set it to None (a null reference). You don’t need a realistic sky for this game, and will use alternative lighting.
4. In the Environment section, set the Environment Lighting Source to Gradient.
The Gradient is broken into three color fields:
Sky, which controls the environment light that comes from above the scene.
Equator, which controls the light that travels from the horizon towards the middle of the scene.
Ground, which controls the light that comes from below the scene.
You can think of the gradient as a big sphere that wraps around your whole Scene.
5. Set the Sky color to a lighter grey: (170, 180, 200)
This will lift the overall brightness of the floors and objects like beds and tables.
6. Set the Equator color to a blue-grey: (90, 110, 130)
This will lift the brightness of the walls and props.
7. Set the Ground color to black: (0, 0, 0)
The Ground color can add up-light that would create a nice Global Illumination-style effect. However, this would be too bright for the haunted house you are creating.
8. Save your Scene.
You’ve completed the basic lighting for your game and used it to enhance its spooky atmosphere. Next, you’ll create a mesh that will help ghosts move around the game environment.
We all know that haunted houses have ghosts roaming the halls — yours should be no different!
To help our ghosts find their way around, Unity has a built-in system called the NavMesh (short for Navigation Mesh). In the second tutorial, you learned that a Mesh is a collection of triangles that all fit together to define a shape. This mesh enables JohnLemon to be rendered to the screen. The NavMesh is an invisible shape over the ground that defines an area within which selected GameObjects can move.
So, how do you decide which areas can and can’t be moved over?
Mark GameObjects as Static
When a GameObject is identified as Static, Unity’s navigation systems assume that it will not move. Your game’s environment is made up of many GameObjects with many Mesh Renderer Components. The combination of all the meshes from the Mesh Renderer Components whose GameObjects are marked as Static form the basis for the NavMesh.
To mark your environmental GameObjects as static:
1. In the Hierarchy, select the Level GameObject.
2. In the Inspector, enable the Static checkbox.
3. A dialogue box will appear asking whether you wish to enable the static flags for all the child objects as well. Select Yes, change children.
4. Now the Level GameObject and all of its child GameObjects are marked as static, but you need to set one exception. There is a Ceiling Plane GameObject in the level design which is being used to cast shadows. If you include this in the bake, the ghosts might end up walking on the ceiling! As spooky as that sounds, it won’t work for this game.
In the Hierarchy window, expand the Level GameObject and its children.
5. Go to Level > Corridors > Dressing > Ceiling Plane and select the Ceiling Pane GameObject.
6. In the Inspector, disable the Static checkbox.
The process of creating a Nav Mesh is called baking. This is done from the Navigation window.
To bake your NavMesh:
1. In the Menu bar, go to Window > AI > Navigation to open the Navigation window. The window should dock itself with the Inspector window. If it doesn’t, drag and dock it there.
2. There are 4 tabs at the top of the Navigation window: Agents, Areas, Bake and Object.
7. Go to File > Save and save your Scene.
You may have noticed that you’re making these changes within the Scene rather than the Level Prefab — you’re overriding the Prefab’s settings without changing it. This is very useful when you want to make small changes to a standard template.
Select the Bake tab.
3. The Bake settings control the details of how the NavMesh will be constructed. The first settings refer to the agents that will traverse (the ghosts that will move around) the NavMesh — the NavMesh Agents. They specifically refer to the size of the agents and the terrain they can move across.
The only setting you need to adjust for your game is the Agent Radius; the ghosts roaming the haunted house will be smaller than the default.
Change the Agent Radius to 0.25.
4. Select the Bake button at the bottom of the window.
The bake process will take from a second to a few minutes, depending on the power of your computer. When it finishes, the environment in the Scene window will be covered in a light blue mesh. This is the area of the environment that the ghosts will be able to move around.
In this tutorial, you added an engaging environment for JohnLemon to escape from and lit it appropriately. You also baked a NavMesh, so enemies will be able to move around the environment.
Your game is starting to take shape and look good! Now it’s time to get the camera moving and add post-processing effects that will make everything in the Scene look even better.
The NavMesh will only be visible when the Navigation window is open and active (if you switch to the Inspector tab, the mesh will disappear from the Scene view). Don’t worry — even when you can’t see the NavMesh, it’s still there!
5. Remember to save your Scene to avoid losing any changes.