Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 5.x By Example

You're reading from   Unity 5.x By Example An example-based practical guide to get you up and running with Unity 5.x

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher Packt
ISBN-13 9781785888380
Length 402 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Alan Thorn Alan Thorn
Author Profile Icon Alan Thorn
Alan Thorn
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. The Coin Collection Game – Part 1 FREE CHAPTER 2. Project A – the Collection Game Continued 3. Project B – the Space Shooter 4. Continuing the Space Shooter 5. Project C – a 2D Adventure 6. Continuing the 2D Adventure 7. Project D – Intelligent Enemies 8. Continuing with Intelligent Enemies Index

Lighting and sky

The basic level has been created in terms of architectural models and layout; this was achieved using only a few mesh assets and some basic tools. Nevertheless, these tools are powerful and offer us a multitude of combinations and options to create great variety and believability in game worlds. One important ingredient is missing for us, however. This ingredient is lighting. You'll notice from Figure 1.34 that everything looks relatively flat, with no highlights, shadows, or light or dark areas. This is because scene lighting is not properly configured for best results, even though we already have a light in the scene, which was created initially by default.

Let's start setting the scene for the coin collection game by enabling the sky, if it's not already enabled. To do this, click on the Extras drop-down menu from the top toolbar in the Scene viewport. From the context menu, select Skybox to enable Skybox viewing. A Skybox simply refers to a large cube that surrounds the whole scene. Each interior side has a continuous texture (image) applied to simulate the appearance of a surrounding sky. For this reason, clicking the Skybox option displays a default sky in the Scene viewport. See Figure 1.35:

Lighting and sky

Figure 1.35: Enabling the sky

Now, although the Skybox is now enabled and the scene looks better than before, it's still not being illuminated properly—the objects lack shadows and highlights. To fix this, be sure that lighting is enabled for the scene by toggling on the Lighting icon at the top of the Scene viewport. See Figure 1.36. This setting is for display purposes only. It only affects whether lighting effects are shown in the Scene viewport and not whether lighting is truly enabled for the final game.

Lighting and sky

Figure 1.36: Enabling scene lighting in the Scene viewport

Enabling lighting display for the viewport will result in some differences to the scene appearance and, again, the scene should look better than before. You can confirm that scene lighting is taking effect by selecting Directional Light from the Hierarchy panel and rotating it. Doing this controls the time of day, rotating the light cycles between day and night and changing the light intensity and mood. This changes how the scene is rendered. See Figure 1.37:

Lighting and sky

Figure 1.37: Rotating the scene directional light changes the time of day

Let's undo any rotations to the Directional Light by pressing Ctrl + Z on the keyboard. To prepare for final and optimal lighting, all non-movable objects in the scene (such as walls, floors, chairs, tables, ceilings, grass, hills, towers, and more) should be marked as Static. This signifies to Unity that the objects will never move, no matter what happens during gameplay. By marking non-movable objects ahead of time, you can help Unity optimize the way it renders and lights a scene. To mark objects as Static, simply select all non-movable objects (which includes practically the entire level so far), and then enable the Static checkbox via the Object Inspector. Note that you don't need to enable the Static setting for each object separately. By holding down the Shift key while selecting objects, you can select multiple objects together, allowing you to adjust their properties as a batch through the Object Inspector. See Figure 1.38:

Lighting and sky

Figure 1.38: Enabling the Static option for multiple non-movable objects improves lighting and performance

When you enable the Static checkbox for geometry, Unity auto-calculates scene lighting in the background—effects such as shadows, indirect illumination, and more. It generates a batch of data called the GI Cache, featuring Light Propagation Paths, which instructs Unity how light rays should bounce and move around the scene to achieve greater realism. Even so, enabling the Static checkbox as we've done still won't produce cast shadows for objects, and this seriously detracts from realism. This happens because most mesh objects have the Cast Shadows option disabled. To fix this, select all meshes in the scene. Then, from the Object Inspector, click on the Cast Shadows checkbox from the Mesh Renderer component, and choose the On option from the context menu. When you do this, all mesh objects should be casting shadows. See Figure 1.39:

Lighting and sky

Figure 1.39: Enabling cast shadows from the Mesh Renderer component

Voila! Your meshes now cast shadows. Splendid work: in reaching this far, you've created a new project, populated a scene with meshes, and successfully illuminated them with directional lighting. That's excellent. However, it'd be even better if we could explore our environment in the first-person mode. We'll see how next.

You have been reading a chapter from
Unity 5.x By Example
Published in: Mar 2016
Publisher: Packt
ISBN-13: 9781785888380
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image