Business

How to Assemble Washers, Bolts & Nuts

Player Class

Next we’re going to create the basic Player class. In our Mono Behaviors folder, create a new C# script called Player. This Player class will start out extremely simple, but we’ll add functionality to it as we go along.

Focus on Prefabs

Life isn’t all fun and games for our adventurer and even intrepid heroes need to make a living somehow. Let’s create some coins in the scene for her to pick up. From the downloaded game assets folder for this book, select the sprite sheet titled, “hearts-and-coins32x32.png”, which totally sounds like a 1980s glam-rock metal band, and drag it into the Assets ➤ Sprites ➤ Objects folder.

Create a Coin Prefab

In this section, we’re going to create the Coin prefab itself. Create a new Game Object in the project view and rename it to Coin Object. Select the four individual coin sprites from the sliced heart coin-fire sprite sheet and drag them onto the Coin Object to create a new animation. Follow the same steps from when we created the Player and Enemy animations. Rename the animation clip to “coin-spin” and save it to the Animations ➤ Animations folder. Rename the generated Controller, “Coin Controller” and move it to the Controllers folder.

Set Up the Circle Collider 2D

Select the Coin Object again and add a Circle Collider 2D component to it. A Circle Collider 2D is a type of primitive collider that we’ll use to detect when a player runs into the coin. Set the Radius of the Circle Collider 2D to: 0.17, so it’s approximately the same size as the Sprite. The script logic we’re about to write requires the player to move through the coin to pick it up. To allow this, we’ll use the Circle Collider 2D a bit differently than we’ve used other Colliders. If we simply added a Circle Collider 2D to the Coin Object, the player wouldn’t be able to walk through it. We want the Circle Collider 2D on the Coin Object to act as a sort of “trigger” and detect when another Collider interacts with it. We don’t want the Circle Collider 2D to stop the other Collider from moving through it.

Layer-Based Collision Detection

We want to give the player in our RPG the ability to pick up coins by walking into them. Our game will also have enemies walking around the map, but we want the enemies to walk right through the coins without picking them up. As we discussed, Layers are used to define collections of Game Objects. Collider components that are attached to Game Objects on the same Layer will be aware of each other and can interact. We can create logic based off of these interactions to do things such as pick up objects. There’s also a technique to make Collider components on different layers aware of each other. This approach uses a Unity feature called Layer-Based Collision Detection.

Triggers and Scripting

As we touched on earlier, Colliders aren’t used only to detect that two objects have run into one another. Colliders also can be used to define a range around an object and to detect that another Game Object has entered that range. When another Game Object is within range, scripted behaviors can be triggered accordingly. The “Is Trigger” property is used to detect when another object has entered the range defined by the Collider. When the player’s collider touches the coin’s circle collider, the method: void On Trigger Enter2D (Collider2D collision) is automatically called on both objects attached to the colliders. We can use this method to customize the behavior that should occur when two objects collide. Because we’re setting Is Trigger, the colliders do not prevent the player from walking through the coin any more.

Scriptable Objects

Scriptable Objects are an important concept to learn for any Unity game developer looking to build a clean game architecture. Scriptable Objects can be thought of reusable data containers that are defined via C# script, generated via the Asset menu, and saved in a Unity project as Assets.

Summary

In this chapter, we’ve started to assemble the various Unity elements into working game mechanics. We’ve built the foundational C# scripts that will be used for all character types in our game, as well as created several types of prefabs that the player can interact with. Collision detection is a fundamental aspect of game development, and we’ve learned about the tools the Unity Engine provides to detect and customize collision detection. We’ve also learned about Scriptable Objects, which are reusable data containers that make our game architecture cleaner.

About admin

Check Also

Essential Tools for HR professionals

Human Resources is responsible for managing and catering to the needs of every employee in …

Leave a Reply

Your email address will not be published.