Chapters

Hide chapters

Unity Apprentice

First Edition · Unity 2020.3.x LTS Release · C# · Unity

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

5. Setting Up a Scene
Written by Eric Van de Kerckhove

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

In the previous section, you discovered the basics of GameObjects and prefabs. You also took the first steps toward creating your own components by using scripting in Unity. That will come in handy in the following chapters as you explore how to use scriptable objects to create a dialogue system.

In this chapter, you’re going to take a closer look at how to set up a scene to make it look appealing.

Just like in a movie set, a scene in Unity needs a camera and lights, so that’s what you’ll focus on. You’re also going to learn more about shadows and the different types of lighting modes. This will mark the start of a game that takes the player to a dining hall populated with vegetable gladiators preparing for the next battle.

Getting started

First, open the starter project for this chapter in Unity and take a look at the Project view. Just like in the previous two chapters, the assets are neatly grouped by their uses in the RW folder.

This time around, there are a few more folders to cover. Here’s a brief overview:

  • Animation: This folder contains the animations and controllers for the vegetable warriors and a treasure chest.
  • Fonts: The main font used for all user interfaces is in here.
  • Materials: The folder containing all the material files that hold the data for what a 3D model should look like.
  • Models: All 3D models live in here.
  • Music: This contains two sweet soundtracks by Kevin MacLeod.
  • Prefabs: This folder holds all the prefabs, organized per category.
  • Scenes: There’s only one scene asset in here for now — DiningHall.
  • Scripts: A few scripts that include a simple interaction system for later use and a script that makes an image blink on and off.
  • Sounds: There are two sound effects in here that will be used for the treasure chest.
  • Sprites: This folder has a single image in it that will act as a cursor in menus.
  • Textures: A folder containing image files that are used as the textures for the 3D models.

Open the DiningHall scene if it isn’t opened yet, and take a look around in the Scene view. It might be hard to tell because it’s quite dark, but there’s a single medieval-looking room with stone walls and wooden tables and benches.

You’ll turn this into a lively dining hall throughout this chapter, even though it might look more like scenery out of Dark Souls at the moment.

Finally, take a look at the Hierarchy to see what GameObjects are present.

There are three GameObjects at the root of the Hierarchy:

  • Props: The term props comes from the film industry and means movable items like hats, knives, carpets, barrels, crates and so on. In the DiningHall scene, these are objects like the food on the dishes and the torches. This GameObject acts like a folder, so expand it fully to see all of its children.
  • Scenery: This is another folder GameObject with a lot of children and grandchildren. These are GameObjects that won’t move like walls, pillars and stairs.
  • Music: This GameObject has an Audio Source attached that loops a soundtrack.

With the project tour out of the way, it’s time to get cracking. Lights, camera, action!

Camera

Every new scene you create starts with a single camera and a directional light — but what do these GameObjects do, exactly? To illustrate why you need these objects, the DiningHall scene doesn’t start with either of these. If you take a look at the Game view, you’ll notice there’s just a black background and a text saying “Display 1 No cameras rendering”.

Adding a camera

Do you hear that? Listen carefully… the sound of total silence. There should be a merry soundtrack playing in the background as the Music GameObject has a looping Audio Source attached to it. What gives? Well, you can’t hear anything if you don’t have ears, and it’s the same in a Unity scene. There might not be an ears component, but there sure is an Audio Listener available to listen to audio sources. This can be found on a camera.

Setting up the camera

To give Main Camera a better view of the scene, adjust its Transform’s position and rotation to (X:26, Y:5, Z:4) and (X:25, Y:-90, Z:0) respectively.

Lighting a scene

Until now, the scene has been quite dark except for the light coming from the fire pits. To fix that, you’ll add some lights to make the room more appropriate to use as a dining hall instead of a moody dungeon.

Types of lights

Unity comes with four types of light for you to use. Here’s a quick primer:

Adding lights

To start, add a new empty GameObject named Lights to the root of the Hierarchy and reset its Transform component. Next, add a Directional Light GameObject by clicking the + button at the top left and selecting Light ▸ Directional Light.

Directional lights

This Light component is shared between all types of light. The only difference is the Type property. This is how you can change a light to be of the type you need — in this case Directional.

Light modes

Next up is the light Mode, which can be set to Realtime, Mixed or Baked. In this scene, all lights will be realtime — which means the lighting and shadows will be recalculated every frame by the game engine. While this may be more heavy on the CPU, it allows objects to move and animate freely while their shadows and lighting look correct at all times.

Light intensity and shadows

Next up in the list of Light component properties is the Intensity and Indirect Multiplier. The intensity of the light can be set with the Intensity property. The higher the value, the brighter the light will be. The default value of 1 is fine for this scene, but you might want to tone it down for night scenes or up the value a bit for extremely bright scenes. Increasing this value too much will make the scene look washed out, so be careful not to overdo it.

Point lights

If you play the scene, you’ll notice there are a bunch of torches hanging against the walls of the dining room. These have fiery particles, but they don’t emit any light yet.

Adding characters

The characters in this scene will be veggie gladiators — fierce warriors that fight daily in the arena. They are getting themselves ready by munching on the fine food and drinks provided. The character the player will control is known as an avatar, a graphical representation of the player in the virtual world. The avatar in this game is a veggie gladiator himself that will be able to interact with the world around him in later chapters. For now, he will be a character that’s just standing around.

Key points

  • You need a Camera component to render the scene to the Game view. To hear sounds, you need an Audio Listener component.
  • A camera generates frames. These are like pictures that are taken at a swift rate. The framerate is the amount of these frames that can be generated per second.
  • The target framerate is often 60 to 120 frames per second (FPS). This is only 16 to 8 milliseconds per frame!
  • Lights allow you to see models in the scene, and they can create shadows to improve the contrast and realism.
  • Point lights shine light evenly like a sphere of light.
  • Spot lights emit light from a single point in a cone shape.
  • Directional lights are like the sun — they shine from infinitely far away in a direction.
  • The Realtime lighting mode allows you to move objects in real time and the shadows will move with them. This is heavier on the CPU.
  • Baked lighting mode bakes the shadows and lights in the scene, which takes a long time. After moving objects, you need to bake again. Baked lighting is easy on the CPU.
  • The Mixed lighting mode combines realtime and baked lighting. The performance lies between realtime and baked.
  • Shadows add a lot to a scene and can be either hard or soft. The latter is a bit more expensive on the GPU.
  • Play around with the shadow Strength and Resolution properties to fit your game and target device.
  • Use the shadow Bias and Normal Bias to fix common rendering issues.
  • An avatar is a graphical representation of a player in a game.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now