Reader small image

You're reading from  Learning C# by Developing Games with Unity - Seventh Edition

Product typeBook
Published inNov 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781837636877
Edition7th Edition
Languages
Tools
Right arrow
Author (1)
Harrison Ferrone
Harrison Ferrone
author image
Harrison Ferrone

Harrison Ferrone is an instructional content creator for LinkedIn Learning and Pluralsight, tech editor for the Ray Wenderlich website, and used to write technical documentation on the Mixed Reality team at Microsoft. He is a graduate of the University of Colorado at Boulder and Columbia College, Chicago. After a few years as an iOS developer at small start-ups, and one Fortune 500 company, he fell into a teaching career and never looked back.
Read more about Harrison Ferrone

Right arrow

Scripting Game Mechanics

In the last chapter, we focused on using code to move the player and camera, with a trip into Unity physics on the side. However, controlling a playable character isn’t enough to make a compelling game; in fact, it’s probably the one area that remains fairly constant across different titles.

A game’s unique spark comes from its core mechanics, and the feeling of power and agency those mechanics give to the players. Without fun and engrossing ways to affect the virtual environment you’ve created, your game doesn’t stand a chance of repeat play, to say nothing of fun. As we venture into implementing the game’s mechanics, we’ll also be upgrading our knowledge of C# and its intermediate-level features.

This chapter will build on the Hero Born prototype by focusing on individually implemented game mechanics, as well as the basics of system design and user interfaces (UIs). You’ll be diving into the...

Adding jumps

Remember from the last chapter that Rigidbody components add simulated real-world physics to GameObjects, and Collider components interact with each other using Rigidbody objects.

Another great thing that we didn’t discuss in the previous chapter about using a Rigidbody component to control player movement is that we can easily add in different mechanics that rely on applied force, such as jumping. In this section, we’ll get our player jumping and write our first utility function.

A utility function is a class method that performs some kind of grunt work so that we don’t clutter up gameplay code—for instance, wanting to check whether the player capsule is touching the ground to jump.

Before that, you’ll need to get acquainted with a new data type called enumerations, which you’ll do in the following section.

Introducing enumerations

By definition, an enumeration type is a set, or collection, of...

Shooting projectiles

Shooting mechanics are so common that it’s hard to think of a first-person game without some variation present, and Hero Born is no different. In this section, we’ll talk about how to instantiate GameObjects from Prefabs while the game is running, and use the skills we’ve learned to propel them forward using Unity physics.

Instantiating objects

The concept of instantiating a GameObject in the game is similar to instantiating an instance of a class—both require starting values so that C# knows what kind of object we want to create and where it needs to be created. To create objects in the scene at runtime, we use the GameObject.Instantiate() method and provide a Prefab object, a starting position, and a starting rotation.

Essentially, we can tell Unity to create a given object with all its components and scripts at this spot, looking in this direction, and then manipulate it as needed once it’s born in the 3D space...

Creating a game manager

A common misconception when learning to program is that all variables should automatically be made public, but in general, this is not a good idea. In my experience, variables should be thought of as protected and private from the start, and only made public if necessary. One way you’ll see experienced programmers protect their data is through manager classes, and since we want to build good habits, we’ll be following suit. Think of manager classes as a funnel where important variables and methods can be accessed safely.

When I say safely, I mean just that, which might seem unfamiliar in a programming context. However, when you have different classes communicating and updating data with each other, things can get messy. That’s why having a single contact point, such as a manager class, can keep this to a minimum. We’ll get into how to do that effectively in the following section.

Tracking player properties

Hero Born is...

Creating a GUI

At this point, we have several scripts working together to give players access to movement, jumping, collecting, and shooting mechanics. However, we’re still missing any kind of display or visual cue that shows our player’s stats, as well as a way to win and lose the game. We’ll focus on these two topics as we close out this last section.

Displaying player stats

UIs are the visual components of any computer system. The cursor, folder icons, and programs on your laptop are all UI elements. For our game, we want a simple display to let our players know how many items they’ve collected and their current health, and a textbox to give them updates when certain events happen.

UI elements in Unity can be added in the following two ways:

  • Unity UI (uGUI)
  • UI Toolkit

uGUI is an older UI system in Unity, but we’re going to use it over UI Toolkit because it’s based on GameObjects that can be easily manipulated...

Summary

Congratulations! Hero Born is now a playable prototype. We implemented jumping and shooting mechanics, managed physics collisions and spawning objects, and added in a few basic UI elements to display feedback. We even got as far as resetting the level when the player wins.

A lot of new topics were introduced in this chapter, and it’s important to go back and make sure you understand what went into the code we wrote. Pay special attention to our discussions on enumerations, get and set properties, and namespaces. From here on in, the code is only going to get more complex as we dive further into the possibilities of the C# language.

In the next chapter, we’ll start working on getting our enemy GameObjects to take notice of our player when we get too close, resulting in a follow-and-shoot protocol that will up the stakes for our player.

Pop quiz – working with mechanics

  1. What type of data do enumerations store?
  2. How would you create a copy of a Prefab GameObject in an active scene?
  3. Which variable properties allow you to add functionality when their values are referenced or modified?
  4. Which Unity method displays all UI objects in the scene?

Don’t forget to check your answers against mine in the Pop Quiz Answers appendix to see how you did!

Join us on discord!

Read this book alongside other users, Unity game development experts and the author himself.

Ask questions, provide solutions to other readers, chat with the author via. Ask Me Anything sessions and much more.

Scan the QR code or visit the link to join the community.

https://packt.link/csharpwithunity

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning C# by Developing Games with Unity - Seventh Edition
Published in: Nov 2022Publisher: PacktISBN-13: 9781837636877
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Harrison Ferrone

Harrison Ferrone is an instructional content creator for LinkedIn Learning and Pluralsight, tech editor for the Ray Wenderlich website, and used to write technical documentation on the Mixed Reality team at Microsoft. He is a graduate of the University of Colorado at Boulder and Columbia College, Chicago. After a few years as an iOS developer at small start-ups, and one Fortune 500 company, he fell into a teaching career and never looked back.
Read more about Harrison Ferrone