DEV Community

Scott Slatton
Scott Slatton

Posted on

A Quick Look into Gameplay Programming with Unity

Since graduating Flatiron School back in May of this year, I have put a lot of my focus in coding on my personal passion: game development. I learned C# in order to script in Unity, a 3D game development program that I learned how to use back in college. However I was never properly taught how to code before my bootcamp experience so I’d say that my eyes have been opened like never before. Now I’d like to share with you some of the things I’ve learned over the past few months.

Lets jump right in!

What Are Components?

In Unity there are bits of code that can be coupled on individual objects. These bits of code are called components, because they are modular and can be appended on to many different types of game objects in order to increase functionality. You can think of a component as a ball and socket system where components can interface with other components extremely smoothly.

When you’re developing gameplay systems in Unity you are basically putting these components together in a cohesive manner so that they talk to each other and create a fun gameplay loop for the player. Unity uses C# in order to write scripts that function as components on these objects, so you can not only make a simple health variable, but you can create a regenerative health system that can be applied to anything with a simple drag and drop.

Component Example

Getting User Input

To get user input for movement is actually very simple. In Unity, you just need to access the Input class and specify by String which Input you’d like to access.

Making Variables Available Inside the Inspector

You can make variables inside your script accessible from the outside in the Inspector by using the “Public” flag in front of your variable declarations.

As you can see in the script above we've made our movement speed accessible in the inspector.

Accessible Variable Example

Not only can we utilize the “Public” keyword to make integers, floats and strings accessible, we can make components and therefore their properties accessible as well.

Collisions

Collisions are where Sir Isaac Newton steps in and adds some fun physics to our game. You can build entire games from just Rigidbody collision mechanics (See Angry Birds). Like much of everything in Unity Colliders come as components, specifically Rigidbody and Colliders.

From the Unity Docs:

A Rigidbody is the main component that enables physical behaviour for a GameObject. With a Rigidbody attached, the object will immediately respond to gravity. If one or more Collider components are also added, the GameObject is moved by incoming collisions.

Colliders on the other hand are the invisible boundaries that actually bump into each other.

So if we want our game objects to be influenced by physics, we need both a Rigidbody component and a Collider. If you don’t have a Collider, objects will be able to pass through other objects. If you are missing a Rigidbody, the object will not be influenced by physics but will repel other objects with Colliders. This is an important feature because we can make our aspects of our level, like the ground and closed doors, impassable for our players.

Those are a few of the tools we can leverage in Unity to create the games that people love to play. Next blog post I’ll go into Triggers, Raycasting, and interactions between different scripts.

Top comments (1)

Collapse
 
missamarakay profile image
Amara Graham

Great intro on the basics!