Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

A guest post by Mike Geig, an experienced teacher and game developer, with a foot firmly in both camps who currently teaches game design and development at Stark State College and the Cleveland Institute of Art. Mike also works as a screencaster for Unity Technologies and is a member of Unity’s Learn department.

A game without any player interaction would just be a movie. Therefore, it is quite important, as a game developer, for you to correctly and efficiently handle inputs from the player. Unity provides several ways to read player input, and it is important to understand them.

Input Class

Virtually all player input in Unity is accessed via the static methods of the Input class. Every script that you write that inherits from Monobehavior will have access to this class. Using Input, you can read buttons, input axes, mouse movements, accelerometer, gyroscope information, and multi-touch screen presses.

Input Keys

Depending upon the systems you have used in the past, reading key presses might seem like the most straightforward and logic method of obtaining player input. Using this method, you will search for the specific keys that you want to read from the player. Let’s say you wanted to use the space bar as the trigger for a player jumping. You could write something like:

This certainly works, though it has a very glaring shortcoming that we will examine shortly.

Input Buttons

Another way you can find player input is by looking for buttons. The big difference between keys and buttons is that keys are specific entities, while buttons are generic. For instance, if you want to fire a bullet every time the player presses the left mouse button you can write:

As you can see, this is more generic in nature. By default, Unity maps the “Fire1” button to the left mouse button. The specific reason why this is a good idea will be covered soon (begin formulating your hypothesis now!).

Input Axes

Unity implements an idea of “input axes.” Unlike a button that only has a down and up position, input axes have a range of values between -1 and 1. The most common ways to use these is for player movement. Consider the “Horizontal” input axis. By default, Unity maps the key ‘A’ to the negative horizontal axis, and ‘D’ to the positive horizontal axis (Think of the WASD First Person Shooter standard controls). Therefore, you have a line of code such as:

This will give you 5 while pressing the ‘D’ key and -5 while pressing the ‘A’ key. Since axes are controlled by two keys instead of just one, they offer a greater level of control to the player.

Keys versus Buttons and Axes

So now it is time to talk about why you should always use buttons and axes instead of keys. The reason buttons and axes exist is for flexibility and player convenience. Consider a situation where you were specifically looking for the space bar for jumping. This works fine, until you decide that you want to use a different keyboard key to handle jumping. At that point you would need to find all instances of using that key and update it. Instead, if you used the default “Jump” button, you could simply remap it in the Input Manager and be on your way.

Next, consider input from the perspective of the player. Many players like to remap controls to better suit their play style. Furthermore, some players have disabilities that prevent them from using standard controllers. For them, remapping is a must. If you control inputs using keys, these players will be sunk. If you use buttons and keys, however, the players can easily remap them using Unity’s player controls.

Input Manager

Now that we’ve settled on buttons and axes as being the better way to handle input, it is a good idea to get familiar with the Input Manager. The Input Manager can be accessed by clicking Edit->Project Settings->Input. The input manager can be used to remap the default buttons and axes to whatever you would like. Additionally, it can be used to create your own buttons and axes, and you now have what you need to do so.

Look below for some great Unity resources from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Learning Game Development with Unity 3D introduces you to the fundamentals of 3D game design with the Unity engine. This tutorial is designed with the absolute beginner in mind; no prior experience with Unity is required.
Advanced Unity 3D Game Development teaches you how to use some of the advanced features available to you within the Unity 4 game engine. This course is designed for the experienced Unity developer. You should have a working understanding of the Unity 4 engine and features before taking this tutorial.
Learning C# by Developing Games with Unity 3D Beginner’s Guide starts by explaining in simple terms the three concepts you need for writing C# code and scripts: 1) variables to hold information; 2) methods (functions) to use the information; and 3) Dot Syntax to communicate the information where it’s needed. The book builds on these concepts to open up the world of C# coding and Unity scripting. You will use this new power to access the features provided in Unity’s Scripting Reference.
Game Development Essentials with Unity 4 LiveLessons demonstrates the power and versatility of the Unity 4 engine and helps you leverage this engine in your own game development endeavors. Geig covers the Unity interface, concepts of 2D and 3D game development, building terrain for your games, as well as developing game objects that interact through collision. You will also learn to work with scripts and manipulate objects through code. And for those of you who want to develop for mobile devices, you will find coverage here as well. Finally, the course ends with a lesson on how to construct your own game with the Unity 4 game engine.
Learn Unity 4 for iOS Game Development takes you through the complete process of Unity iOS game development. A game developer for over 12 years, the author presents production-proven techniques and valuable tips and tricks needed to plan, build, test, and launch games for the iPhone, iPod, and iPad. He walks you through all the necessary procedures, including how to publish your game to the App Store.

About the author

Me-1131-Edit Mike Geig is both an experienced teacher and game developer, with a foot firmly in both camps. He currently teaches game design and development at Stark State College and the Cleveland Institute of Art. Mike also works as a screencaster for Unity Technologies and is a member of Unity’s Learn department. His Pearson video, Game Development Essentials with Unity 4 LiveLessons is a key title on Unity. Mike was once set on fire and has over a million “likes” on Facebook and can be reached at @mikegeig.

Tags: axes, buttons, Input class, Input keys, Input Manager, Monobehavior, player input, Unity,

Comments are closed.