Foundations

Now that we’re familiar with the Unity Editor, it’s time to starting making our game. This chapter will walk you through how to construct the objects and write the code that will comprise our game. We’ll talk about software design patterns used in Unity,

  • PDF / 5,100,445 Bytes
  • 60 Pages / 439.37 x 666.142 pts Page_size
  • 5 Downloads / 287 Views

DOWNLOAD

REPORT


Foundations Now that we’re familiar with the Unity Editor, it’s time to starting making our game. This chapter will walk you through how to construct the objects and write the code that will comprise our game. We’ll talk about software design patterns used in Unity, along with some higher-level principles in Computer Science and how they’re relevant to making games. You’ll also learn how to control the player on-screen and play the player animations.

Game Objects: Our Container Entities Games in Unity consist of Scenes, and everything in a Scene is called a GameObject. You’ll encounter Scripts, Colliders, and other types of elements in your Unity adventures, and all of these are GameObjects. It’s helpful to think of GameObjects as a sort of container, composed of many pieces of individually implemented functionalities. As we discussed in Chapter 2, GameObjects can even contain other GameObjects in parent– child relationships. We’re going to create our first GameObject, then talk about why Unity uses GameObjects as a fundamental aspect of building games. In the Hierarchy view, select the Create button in the top-left (Figure 3-­1), and select Create Empty. This creates a new GameObject in the Hierarchy view.

© Jared Halpern 2019 J. Halpern, Developing 2D Games with Unity, https://doi.org/10.1007/978-1-4842-3772-4_3

31

Chapter 3

Foundations

Figure 3-1.  One way of creating a new GameObject in the Hierarchy view There are a few different ways to create GameObjects. You also could have right-clicked on the Hierarchy view pane itself, or gone to the GameObject ➤ Create Empty in the top menu. Right-click the new GameObject and select Rename. Call it “PlayerObject.” This PlayerObject will contain all the logic related to the courageous player in our RPG! Make a second GameObject and call it “EnemyObject.” This EnemyObject will contain all the logic related to an enemy that our player must defeat. As we learn how to build a game in Unity, we’re also going to learn Computer Science concepts that will make you a better programmer overall, and how those concepts will make your life easier as a game developer.

32

Chapter 3

Foundations

Entity-Component Design There is a concept in Computer Science known as “separation of concerns.” Separation of concerns is a design principle that describes how software is divided into modules based on the functionality they perform. Each module is responsible for a single functional “concern” that should be completely encapsulated by that module. When it comes down to implementation, a concern can be a somewhat loose and interpretive term—these concerns can be as broad as the responsibility for rendering graphics on-screen, or as specific as calculating when one triangle in space overlaps with another triangle. One of the primary motivations for separating concerns in software design is to reduce wastefulness seen when a developer writes duplicated or overlapping functionality. For example, if you have code that renders an image on-screen, you should only have to write that code