Published in 2022 by The Rosen Publishing Group, Inc.
29 East 21st Street, New York, NY 10010
Copyright 2022 by The Rosen Publishing Group, Inc.
First Edition
All rights reserved. No part of this book may be reproduced in any form without permission in writing from the publisher, except by a reviewer.
Library of Congress Cataloging-in-Publication Data
Names: Romphf, Joshua, author.
Title: Coding activities for developing games in Unity / Josh Romphf.
Description: New York: Rosen Publishing Group, Inc., 2022. | Series: Code creator | Includes bibliographical references and index.
Identifiers: LCCN 2019013182| ISBN 9781725341029 (library bound) | ISBN 9781725341012 (pbk.)
Subjects: LCSH: Computer gamesProgrammingJuvenile literature. | Unity (Electronic resource)Juvenile literature.
Classification: LCC QA76.76.C672 R6454 2022 | DDC 794.8/1525dc23
LC record available at https://lccn.loc.gov/2019013182
Manufactured in the United States of America
Some of the images in this book illustrate individuals who are models. The depictions do not imply actual situations or events.
CPSIA Compliance Information: Batch #CSRYA22. For further information contact Rosen Publishing, New York, New York at 1-800-237-9932.
Contents
Introduction
Unity is the most popular game engine on the planet and is a very powerful tool for creating more than just games. It would be impossible to cover everything Unity has to offerlet alone make a fleshed out gamein a few activities. However, these projects will give you all of the pieces you need to begin building interactive experiences of your own. Since Unity has excellent documentation and a very active community, it is possible to find a tremendous amount of resources. In other words, this book will be a great stepping stone for you to develop your programming skills in general and your Unity skills in particular.
It is worth noting that while it is a powerful engine, there are certain things that Unity cannot do. Unity is not a 3D modeling program, so it cannot create models. Similarly, Unity is not designed to rig and animate those models; instead, it works with existing assets that can then be imported into the program.
A game engine is responsible for doing a lot of things. It has to provide a friendly interface to allow developers to import their content and build interactive worlds. It also needs to offer tools to handle user input, physics, networking, artificial intelligence, and a bunch of other things. Oh, and it has to render all of the visuals at an acceptable frame rate and make them look amazing in the process. Luckily, someone has created software to do all of this.
To download Unity, head to https://Unity3d.com and click on the button to get started. Unless you are planning on making tons of money with your game right away, select Try Personal on the following page.
Next, follow the instructions for how to install Unity on your operating system. You will have two options for installing Unity, either through Unity Hub or by downloading an installer. If you want to use Unity Hub, there are useful instructions here: https://docs.Unity3d.com/Manual/GettingStartedInstallingHub.html. If you prefer to use an installer, you can find instructions here: https://docs.Unity3d.com/Manual/InstallingUnity.html.
Every game, from indie gems to blockbuster titles, are based on an engineand Unity is one of the best.
During installation, there may be a prompt that asks if you want to install additional packages. For these activities, you will only need the defaults.
After the installation, you can open the program and start a new project. You should see a launcher that looks for existing projects on your system. Click the New button.
Start off with the 3D template. The rest of these boxes can be filled in with whatever information you choose. With all that sorted out, create a new project. After it loads, you will be met with the Unity Editor interface. Unity has a very useful page that explains how the editor is laid out. Please take a look at this site before moving on to the first activity: https://docs.Unity3d.com/Manual/LearningtheInterface.html.
Activity 1
Intro to 3D Graphics
Before building cool stuff in Unity, it is important to cover basic 3D geometry and graphics concepts and use examples right in Unity itself. One basic element is a 3D coordinate system.
Simply put, a coordinate system is a system that uses numbers to position objects within a given space. Plots and graphs in most math classes are commonly made within a Cartesian coordinate system, which uses sets of numerical coordinates to position points on a plane. These points have coordinates that represent their signed distance (meaning that they can be positive or negative) from two or more fixed perpendicular lines called axes. In a 2D (or twodimensional) Cartesian coordinate system, there are two axes: the x-axis and y-axis.
Where the two axes meet is called the origin. The origin is always located at x = 0 and y = 0. A point can be defined anywhere along those two axes, and is represented as a pair, like this: (x, y).
When working with a three-dimensional Cartesian coordinate system, the z-axis is added.
In Unity, consider this coordinate system to be the world space; lights, cameras, characters, and any other game objects have a position (or point) within this world.
Think of the world as one big cube. The origin of that world is at point (0, 0, 0). In the top right of the scene view in Unity, there is this tool:
This is called the Scene Gizmo, which shows us the current orientation of the Scenes camera. It is also a helpful tool for visualizing 3D world space. Try clicking on the x, y, and z arms of the gizmo to see the corresponding positions of each axis. Take a few seconds to navigate around the scene using a mouse or track pad.
There is one more geometric concept that is important in Unity (and graphics in general): the concept of a vector. Vectors are like points, but they are much more powerful. Vectors are used in things like collision detection, rendering, and even moving objects around in 3D space. Unlike points, vectors have a direction and a magnitude. What does this actually mean? Picture a vector as an arrow with a given length (or magnitude) pointing in a particular direction. Like a point, a vector has values known as components. For instance, a vector in 2D space (Vector2 in Unity) has both an x and a y component. In 3D space (Vector3), it has an x, y, and z component. It may be hard to see the difference between vectors and points, but the math behind vectors allows you to do a lot more. For now, drop a cube into the Scene by selecting GameObject > 3D Object > Cube. In the Inspector panel, there is a chart measuring its transform.
Every GameObject has a transform, which is used to store information about the position, rotation, and scale of a GameObject. Each of the position, scale, and rotation properties is stored as a Vector3. See the x, y, and z values for each of those properties? Those are the components. Now, try changing those values and seeing what each one does to your cube. Now you can see just how important the transform is for moving an object in world space.
Next page