Lesson 47 - Controller Mapping and Input Manager

Tutorial Series: Introduction to Unity with C# Series

Previous Article  |  Next Article


Transcript

Alright. I thought a good feature we can add at this point is controller support. Unity has support for pretty much any kind of input device you can think of, but the most common input device is the common game controller. Here I have an ordinary wireless Xbox 360 gamepad that most of you probably have already, but you can do this lesson with just about any controller that your computer recognizes. The way that controllers work is they first interface with your operating system, which then identifies the controller as a device. Meanwhile, Unity has a built-in InputManager that pulls information from this device service, and you can then map the buttons in this InputManager in order to reference them in code.

The first thing to do is take a look at the Windows controller interface, so in the search bar type in ‘game controllers’. You'll get to a screen that looks like this that identifies your controller. In the Properties, you can find which button number corresponds to the actual button on the controller, so you can just press the buttons and figure it out. Actually, the buttons themselves might be numbered differently in Unity than what you see in the windows device service. If you're using an Xbox 360 controller you'll find the number mappings at this website that's nice and handy and available to get the button mappings as they are in Unity for the InputManager, and I'll be using these mappings for this lesson. OK, so our game here pretty much uses two buttons, plus the directional inputs. I want to use the A and B buttons on the controller for the Teleport and PowerUp controls. Well, we'll deal with the directional inputs separately and at the end also we'll add a start button for the game manager as well. First, go over to Unity and go to Edit, Project Settings, Input, and this will bring up the InputManager. Here buttons are called axis, and you'll see a default set up that's already half configured. Let's erase this default setup by setting the axis to 0 and press enter.

Now, first let's set up the directional axis for horizontal and vertical direction inputs. We want two axis, one for horizontal and one for vertical. Here it says Element 0, we'll give it a name, ‘Horizontal’. Input these settings, which we'll briefly touch on in a moment. Here for the type, just say Joystick Axis, and it'll be on the X-axis for horizontal, of course. Get Motion from all Joysticks should also be selected. Then for this we'll do it very similarly, we'll say ‘Vertical’ for the name. Same settings here. .8, 2. This would be the Y-axis and Joystick Axis yet again. Alright. The only parameter that we're really interested in here is the ‘Dead’ parameter, which represents the dead zone for this axis. .8 dead represents if the analog stick is going 80% of the way to its maximum on the horizontal axis, it just won't register. You have to push the stick 80% of the way or higher for it to register. We're doing that only so that the game is not too touchy, right, with the analog stick. Actually, this kind of game that we're coding here, probably is not best suited to be played with the analog stick, you're probably better off using the D-Pad because it's just an eight way directional game anyways. You're either moving in one direction 100% or 0%, right? There's no in between.

Now, let's reference this directional input in code to wire it all up and get it working. Got to CubeController. Here in out Movement() method, let's add reference to this directional input, so if we're hitting the left arrow, or we use Input.GetAxis(“Horizontal”) the horizontal axis returns true, not only returns true, but is less than zero. Because minus would represent a left movement on the horizontal axis, then execute the movement for the left movement. Here, for the right it'll be the same except for the right, it'll be greater than zero. For the UpArrow it will be the GetAxis(“Vertical”) we’ll refer to the vertical axis and flip this as well. Alright. Let's just run this, and I'll mute the audio so it doesn't get in our way and I can talk. I'll use my gamepad this time to control movement. Alright. Yeah. The analog joystick, it's working. It still feels a little less than optimal because, again, I think this is best suited for digital input from the D-Pad. We’ll worry about that in a second. That's working. Now, going back to the InputManager, you may have noticed this ‘Negative Button’, ‘Positive Button’, and so on. Well, for button inputs, this is mainly for determining the control values if you're using an ordinary button.

For example, for keyboard input, we often use the WASD controls. A and D would be used for the horizontal axis. In this case, the positive button would be A, and D would be the negative button. That's what that's all about. We're not going to make use of keyboard input, we're just going to stick to controller input for now, but I wanted to quickly touch on that. Alright. Apparently if you have a wired gamepad, your directional inputs are mapped the same as your analog stick, but apparently for whatever reason for the wireless controllers the D-Pad inputs are mapped to completely different axis. This is all detailed at that site I mentioned earlier. To map the D-Pad for a wireless controller, add this in the InputManager. Let's add another two axis and make this 4, press enter. For this one we'll say DPad_Horizontal, and we don't need a dead zone because it's digital. The D-Pad, it returns either -1, or 1. It doesn't return a float or an in between value, and leave that as it is. Mostly we'll keep it as Joystick Axis here. For this we'll have to reference a sixth axis, that's the horizontal D-Pad axis, and that should work fine for that. Here we'll say DPad_Vertical, and this will be the seventh axis. Again. This is all detailed on that website. You should reference that if you have an Xbox controller.

I'm going back to the controller movement inputs here.. We'll reference that axis. Either you use the left arrow or the analog stick, or Input. Here, I'll get this out of the way just so we have more real estate here. Unfortunately this is a string that I'm passing in so you have to type it exactly as the name is in the InputManager, or else it just won't work. This would be greater than. This would be DPad_Vertical, and so will that. That would be greater than, right? I'll just quickly test this to make sure that it's coded properly. We use that D-Pad now for movement. I think it feels a lot better than the analog stick. Alright, awesome. Now, to configure the Teleport and PowerUp buttons, back to the InputManager. Type in 6 and press enter. For this we'll call it Teleport. Again, referring to that website with the button mappings for Unity, we'll set the positive button to joystick button 0, it's a key or mouse button. Just set that to the X-axis, it doesn't matter. That should be good for that. Then for PowerUp, call it PowerUp. This would be Positive Button: joystick button one. Again, Key or Mouse Button and just put this on the X-axis, it doesn't matter.

Alright. Now, for the Teleport, we just go to CubeController. In the Teleport() method and put an or, ‘|| ’ for Input.GetButtonDown() instead of GetKeyDown() It's called Teleport, right? For the PowerUp, we go to the PowerUpSpawn, or the PowerUpManager. Here we'll say either KeyCode.Z || Input.GetButtonDown(“PowerUp”) Then we'll wrap this or conditional check, we'll wrap this conditional check in parentheses, so it checks that together, it's either or. The same thing here for the else if. Alright, let's quickly test that out. The Teleport and PowerUp. Alright. The A button should be for teleporting. Yup, that works. Then for the PowerUp, the B button. Great. Alright. Finally, we should also implement the start button to perform the same function as the enter key. In the InputManager, let's make that axis. So, 7, and here we'll call it Start, and it'll be button 7. Now in the GameStartManager, just put in that check ‘|| Input.GetButtonDown(“Start”)’. I'll load the Start scene in order to test this. Alright. Hitting the Start button, here we go. Works. Also at GameOver or when we need to reset the scene, it works as well. Great.


Related Articles in this Tutorial:

Lesson 1 - Who This Course is For

Lesson 2 - What to Expect from this Course

Lesson 3 - Installation and Getting Started

Lesson 4 - Starting the First Project

Lesson 5 - Prototype Workflow

Lesson 6 - Basic Code Review

Lesson 7 - Game Loop Primer

Lesson 8 - Prototyping Continued

Lesson 9 - C# Fundamentals and Hello World

Lesson 10 - Variables and Operations

Lesson 11 - Variables and Operations Continued

Lesson 12 - Floats, Bools and Casting

Lesson 13 - If Statement Conditionals

Lesson 14 - If Statements Continued

Lesson 15 - Complex Evaluations and States

Lesson 16 - Code Syntax vs. Style

Lesson 17 - Variable Scope

Lesson 18 - Object-Oriented Programming Intro

Lesson 19 - OOP, Access Modifiers, Instantiation

Lesson 20 - Object Containment and Method Returns

Lesson 21 - "Has-A" Object Containment

Lesson 22 - "Is-A" Inheritance Containment

Lesson 23 - Static Fields and Methods

Lesson 24 - Method Inputs and Returns

Lesson 25 - Reference vs. Value Types

Lesson 26 - Introduction to Polymorphism

Lesson 27 - Navigating the Unity API

Lesson 28 - Applying What You've Learned and Refactoring

Lesson 29 - Constructors, Local Variables in the Update Method

Lesson 30 - Collecting Collectibles, Items and Powerups

Lesson 31 - Spawning and Managing Prefab Powerups

Lesson 32 - Implementing Powerup State Logic

Lesson 33 - Displaying Text, OnGUI, Method Overloading

Lesson 34 - Referencing Instantiated GameObjects, Parenting

Lesson 35 - Understanding the Lerp Method

Lesson 36 - Creating Pseudo Animations in Code

Lesson 37 - Understanding Generic Classes and Methods

Lesson 38 - Animations Using SpriteSheets and Animator

Lesson 39 - Working with Arrays and Loops

Lesson 40 - Debugging Unity Projects with Visual Studio

Lesson 41 - Camera Movement and LateUpdate

Lesson 42 - Playing Audio Clips

Lesson 43 - Routing Audio, Mixers and Effects

Lesson 44 - Adding Scoring Mechanics and Enhancements

Lesson 45 - Scene Loading and Game Over Manager

Lesson 46 - Understanding Properties

Lesson 47 - Controller Mapping and Input Manager

Lesson 48 - Understanding Enums

Lesson 49 - Dealing with Null References

Lesson 50 - Handling Variable Framerates with time.DeltaTime

Lesson 51 - Preparing the Project for Final Build

Lesson 52 - Final Build and Project Settings

Lesson 53 - Introduction to the Unity Physics Engine

Lesson 54 - Understanding FixedUpdate vs. Update

Lesson 55 - Movement Using Physics

Lesson 56 - Attack Script and Collision Events with OnCollisionEnter2D

Lesson 57 - Projectiles and Stomping Attack

Lesson 58 - Parallax Background and Scrolling Camera

Lesson 59 - Infinitely Tiling Background Sprites

Lesson 60 - OOP Enemy Classes

Lesson 61 - OOP Enemy Classes Continued

Lesson 62 - Trigger Colliders and Causing Damage

Lesson 63 - Multi-Dimensional Arrays and Procedural Platforms

Lesson 64 - Finishing Touches

Lesson 65 - Series Wrap


Comments

Please login or register to add a comment