Essentials of chapter 5 of Game Dev Essentials

Posted on: January 25th, 2015 by tdc_user
I teach a class called Programming for Artists, and we use the book Unity 3.x Game Development Essentials. I sometimes have students who get confused by all the things happening in Chapter 5 so I am going to break it down a bit. That is the reason for this post, Essentials of Chapter 5 of Game Dev Essentials. Think of this as a type of book review as well as a chapter guide. Chapters 1-4 are a mixture of introduction to coding in UnityScript (a type of Javascript), C#, and how to use some of the tools in the Unity game engine for creating terrain, etc. These chapters are well worth reading, and the fact that you can see code samples in both Javascript and C# is a huge plus. Keep in mind much of these first four chapters are conceptual.

Chapter 5 is where the book project starts.

Goldstone starts the chapter with some general statements about tools you can use to model geometry for your games, how to think about scale factors, settings for models, meshes, normals and tangents, materials, animations, and animation compression. All good material, but the actual project starts on page 143 with Setting up the Outpost model. Here are the essential steps to getting the chapter assignment working. What you will be doing is setting up the Outpost so that the door automatically opens and shuts when you enter it from any direction. Obviously you should first be armed with the Unity game engine and a copy of the book before you embark on this exercise. 1. You need to locate and import the book project package so you will have assets to work with. This package is downloadable from Packt Publishing as UGDE-4.x-ImportSet.unitypackage. Assets > Import Package > Custom Package > then go find the package, and when prompted, click on Import all assets. Looking at your Project panel, you will find you have a new folder for assets in your Assets folder called Book Assets. 2. Open the Models folder and drag the Outpost model onto your terrain. The figure at right shows the Outpost model in the scene, and the Outpost is in a “selected” state, so we can see some of its attributes in the Inspector. You can click on the image to enlarge it. inserting the unity outpost
    3. Now there is in the hierarchy an Outpost game object, which also has a child called Outpost (which is a mesh). For clarity, I will refer to the parent as the Outpost parent, and the child as the Outpost child. Select the parent Outpost and add another box collider to it; take your time and adust the position and size of the new Outpost box collider so that it sits over the door (see figure at right – you can click on the image to enlarge it). You are going to use this box collider to detect the presence of the Player. The reason we are attaching this collider to the Outpost and not the door is that we don’t want our detection collider to be moving around (like the door does). Note that we have checked the box called Is Trigger (very important). outpost box collider 4. There is an animation attached to the Outpost that animates the door opening and closing. We don’t want this door animation to play automatically, so select the Outpost parent and uncheck the box that says “Play Automatically”. The book then goes into an explanation of three different ways to potentially detect the player and cause the door to the Outpost to open, from pages 150 to 181. I am going to simplify it here, though I do strongly encourage the reading of this section. The three methods are using the Character Controller Collider, using the Ray Casting, and using Trigger collision detection. To summarize, “Trigger collision detection” is the superior method. The “Character Controller Collider” method is a bad idea because it causes a collision for the player with stops their progress, and adds unnecessary code complexity for the player to carry around. The “Ray Casting” method is a bad idea because it forces the player to turn directly towards the door so that the “ray” hits the door. “Trigger collision detection”, a technique which I covered in a previous blog post, detects anything in the area of the door’s collision area and does not stop the player’s forward progress. 5. Drag a First Person Controller from your Standard Assets > Character Controllers folder into the Scene. Make sure to position the player well and pull them up so they are above the terrain. 6. Add a “Player” tag to your First Person Controller. 7. Create a new script called TriggerZone.js (or you can use C#). Attach this script to the Outpost parent. 8. Create a new script called DoorManager.js (or you can use C#). Attach this script to the door game object (which is a child of the Outpost parent). The door manager script also has two slots for the sound of a door opening and closing – drag these sounds from your Book Assets > Sounds folder to the proper slot in the DoorManager script. See screenshot. door manager script and sounds 9. What the code is doing: Goldstone does an excellent job of explaining how the code works, but let me summarize a few things here: first, TriggerZone.js notices when the Player enters the trigger detection area. Then we see this line of code: transform.FindChild(“door”).SendMessage(“DoorCheck”); Transform, in the above line of code, refers to the transform this script is attached to, which is the Outpost parent. FindChild is a method built into Unity which allows you to efficiently find any children of the parent game object. And we know from looking at the hierarchy that “door” is one of the children of the Outpost parent. Well, now that we have the “door” game object found by Unity, we are going to send a message (.SendMessage) to any script attached to that game object which has a function called “DoorCheck”. So what we are able to do with SendMessage is to launch a function in another script which is attached to any particular game object. Pretty powerful stuff! I will not go into how DoorCheck works here as it is well-covered in the book. 10. Add an AudioSource component to the door; Add Component > Audio > Audio Source. Every time you have a sound attached to a game object, you need an AudioSource component attached to that same component for the sound to play. You do not need to assign any sounds to this component, however; its mere existence is enough. 11. Test the functionality of the door by moving your Player with the mouse and arrow keys to the door. It should open automatically, allowing your Player to enter, and close once you are inside, as well as allowing the Player to likewise exit the building. Tags:

Comments are closed.