Game Objects: Our Container Entities

Game Objects: Our Container Entities

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.

Animations

Let’s create a new folder to hold the animations we’re about to create. You remember how to do that, right? Select Assets from the Project view, right click, and then select Create ➤ Folder. Or you can click the Create button in the top-left of the Project view. Call this folder, “Animations”. Select the Animations folder and create another two subfolders within it, titled “Animations” and “Controllers”. Expand the Player sprites by clicking the little arrow next to it in the Project view. Select the first Player sprite—this should be a sprite of the player walking east. Hold down the shift-key to select the three sprites next to it.

The Animator State Machine

The Animation Controller maintains a set of rules, called a State Machine, used to determine which Animation Clip to play for an associated object based on which state the Player is in. Some examples of states used by a Player object might be: walk, attack, idle, eat, and die. We further divide up these states into directions because our player might be facing north, south, east, or west when they are in these states.

Colliders

Next we’re going to learn about Colliders. Colliders are added to Game Objects and used by the Unity Physics Engine to determine when a collision has taken place between two objects. The shape of a Collider is adjustable, and they’re usually shaped more or less like the outline of the object they represent. It’s sometimes computationally prohibitive to outline the exact shape of an object and often unnecessary, as an approximation of an object’s shape is sufficient for collision purposes and indistinguishable by the player during runtime. An approximation of the objects shape using a type of Collider called a “Primitive Collider” is also less processor intensive. There are two types of Primitive Colliders in Unity 2D: Box Collider 2D and Circle Collider 2D.

The Rigid body Component

A Rigid body Component added to a Game Object allows that Game Object to interact with the Unity Physics Engine. It’s how Unity knows to apply forces such as gravity to a Game Object. A Rigid body also allows you to apply forces to the Game Object via scripts. For example, your game may have a Game Object called “car,” which contains a rigid body. You could apply a certain amount of force to the car object to move it in the current direction, depending on which button a player is pressing: gas or turbo.

Tags and Layers Tags

Tags allow us to label Game Objects for easy reference and comparison while our game is running. The Player tag is a default tag that comes with every Scene in Unity but you also can add tags as you need. Create a new Tag called “Enemy” and use it to set the Enemy Object Tag. We’ll add Tags for other items later as our game develops.

Layers

Layers are used to define collections of Game Objects. These collections are used in collision detection to determine which layers are aware of each other and thus can interact. We can then create logic in a Script to determine what to do when two Game Objects collide. As we can see in Figure 3-22, we want to create a new “User Layer” called “Blocking”. Type “Blocking” into the User Layer 8 field.

Summary

In this chapter we’ve covered a lot of the core knowledge required to make games in Unity. We covered some of the design philosophy and computer science principles behind how Unity works. We covered how games in Unity are made of Scenes, and everything in a Scene is a Game Object. We learned about how Colliders and Rigid body components work together to determine when two Game Objects collide and how Unity’s physics engine should handle the interaction. We learned how Tags are just labels used to refer to Game Objects, such as the Player Object, from Scripts while our game is running. Another useful tool we added to our toolkit is Layers, which are used to group together Game Objects. We can then impose logic onto these Layers via Scripts.

About admin

Check Also

The Rise of Online Toto Games

The Rise of Online Toto Games

In the year 2020, the market for gaming in the world was about $62 million. …

Leave a Reply

Your email address will not be published. Required fields are marked *