Unity Game Development Scripting
Unity Game Development Scripting
Unity Game Development Scripting
C o m m u n i t y
$ 44.99 US
27.99 UK
"Community
Experience
Distilled"
Kyle D'Aoust
D i s t i l l e d
E x p e r i e n c e
Kyle D'Aoust
Chapter 9, Game Settings, covers how to create customizable configurations for audio
and video settings. You'll create the ability to save and load these settings by using
PlayerPrefs.
Chapter 10, Putting It All Together, will put almost everything you've learned from the
previous chapters into a small game. By taking elements from the previous chapters,
you'll create a short First Person Action RPG.
Interactive Input
Before we start creating our game, it is a good idea to figure out our controls.
We'll create a script that will hold our inputs, and create control profiles for both the
keyboard/mouse as well as the Xbox 360 Controller. Then, we'll add functionalities
to be able to switch between the profiles and customize them as well. Control
configurations like these are a key element to games, especially PC games.
In this chapter, we will cover the following topics:
Interactive Input
Keyboard/mouse
Movement
WASD keys
Left thumbstick
Rotate camera
Mouse
Right thumbstick
1234 keys
Directional pad
Inventory
The I key
The A button
Pause game
Aim
[6]
Chapter 1
As you can see, we still need to add inputs for the inventory, pause game, and item
bar buttons. We will also need to make sure that the inputs we enter will support
inputs from the Xbox 360 Controller.
Interactive Input
[8]
Chapter 1
The I key name will be I_Key and its positive button parameter will be i. For the
Esc key, we will want the Name parameter to be Esc_Key and its positive button
parameter will be escape.
This Boolean will be used in later functions to determine whether there is a gamepad
connected. The string will be used to hold the name of the gamepad connected.
[9]
Interactive Input
This function uses the GetJoystickNames function of the input, which gets
and returns an array of strings, which consists of the names of the connected
gamepads. We use this to set our Boolean to true or false; true meaning there's a
device connected and false meaning that the game couldn't detect a device. The
reason we use a try-catch expression is because if there is no gamepad connected
Input.GetJoystickNames() will give you an IndexOutOfRangeException error.
As you can see, we are assigning the name of the gamepad connected to our
Controller variable. To use this function, call it within the DetectController
function, in the if statement, where we set isControllerConnected to true.
[ 10 ]
Chapter 1
We will use these variables to display our controls on the screen. Later, we'll use
them for customization as well. Add this code to assign the default values to our
new variables:
void SetDefaultValues()
{
if(!isControllerConnected)
{
PC_Move = "WASD";
PC_Rotate = "Mouse";
PC_Item1 = "1";
PC_Item2 = "2";
PC_Item3 = "3";
PC_Item4 = "4";
PC_Inv = "I";
PC_Pause = "Escape";
PC_AttackUse = "Left Mouse Button";
PC_Aim = "Right Mouse Button";
}
else
{
PC_Move = "WASD";
PC_Rotate = "Mouse";
PC_Item1 = "1";
PC_Item2 = "2";
PC_Item3 = "3";
PC_Item4 = "4";
PC_Inv = "I";
PC_Pause = "Escape";
PC_AttackUse = "Left Mouse Button";
PC_Aim = "Right Mouse Button";
[ 11 ]
Interactive Input
Xbox_Move = "Left Thumbstick";
Xbox_Rotate = "Right Thumbstick";
Xbox_Item1 = "D-Pad Up";
Xbox_Item2 = "D-Pad Down";
Xbox_Item3 = "D-Pad Left";
Xbox_Item4 = "D-Pad Right";
Xbox_Inv = "A Button";
Xbox_Pause = "Start Button";
Xbox_AttackUse = "Right Trigger";
Xbox_Aim = "Left Trigger";
}
}
We will set these variables in a function because later we will use this function
again to reset the controls if they are customized. The function uses our
isControllerConnected variable to determine whether a gamepad is
plugged in or not, and then assigns the appropriate data.
[ 12 ]
Chapter 1
GUI.Label(new Rect(25, 150, 125, 20), "Item 2: ");
GUI.Button(new Rect(150, 150, 135, 20), PC_Item2);
GUI.Button(new Rect(325, 150, 135, 20), Xbox_Item2);
GUI.Label(new Rect(25, 175, 125, 20), "Item 3: ");
GUI.Button(new Rect(150, 175, 135, 20), PC_Item3);
GUI.Button(new Rect(325, 175, 135, 20), Xbox_Item3);
GUI.Label(new Rect(25, 200, 125, 20), "Item 4: ");
GUI.Button(new Rect(150, 200, 135, 20), PC_Item4);
GUI.Button(new Rect(325, 200, 135, 20), Xbox_Item4);
GUI.Label(new Rect(25, 225, 125, 20), "Inventory: ");
GUI.Button(new Rect(150, 225, 135, 20), PC_Inv);
GUI.Button(new Rect(325, 225, 135, 20), Xbox_Inv);
GUI.Label(new Rect(25, 250, 125, 20), "Pause Game: ");
GUI.Button(new Rect(150, 250, 135, 20), PC_Pause);
GUI.Button(new Rect(325, 250, 135, 20), Xbox_Pause);
GUI.Label(new Rect(25, 275, 125, 20), "Attack/Use: ");
GUI.Button(new Rect(150, 275, 135, 20), PC_AttackUse);
GUI.Button(new Rect(325, 275, 135, 20), Xbox_AttackUse);
GUI.Label(new Rect(25, 300, 125, 20), "Aim: ");
GUI.Button(new Rect(150, 300, 135, 20), PC_Aim);
GUI.Button(new Rect(325, 300, 135, 20), Xbox_Aim);
GUI.EndGroup();
}
The preceding code is fairly self-explanatory; we use GUI labels to show what
actions the player can do, then use the GUI buttons to show what inputs the actions
are mapped to. Later, we'll use these buttons as a way to customize our controls.
Let's switch!
Now, we'll create a way for the player to switch between PC and Xbox
360 Controller controls.
[ 13 ]
Interactive Input
Finally, go to the DetectController() function. Add this line of code before the line
of code where you call the IdentifyController() function in the if statement:
cProfile = ControlProfile.Controller;
After this, add an else statement to the if statement with another line of code
after it:
else
cProfile = ControlProfile.PC;
We can call this function later to let the player choose between using the
keyboard/mouse or the Xbox 360 Controller.
Chapter 1
{
if(cProfile == ControlProfile.Controller)
SwitchProfile(ControlProfile.PC);
else
SwitchProfile(ControlProfile.Controller);
}
The text on the button will display which current control profile is being used.
When the player clicks on the button, it will switch the control profile.
Customization is key
It's time to customize our controls! We'll go over a couple of ways to add customization
to our controls. Unity doesn't allow us to edit the input properties while in-game,
so we will create a couple of ways to change the controls ourselves. In our game,
we will utilize both these ways.
Since we can't modify the input properties, some of our controls will not be
customized, such as movement, camera rotation, Xbox 360 Controller attack/use,
and Xbox 360 Controller item switching. Next, we will need to set some default
values to these variables; we'll modify our SetDefaultValues() function to look
like this:
void SetDefaultValues()
{
ControlScheme = "Scheme A";
if(!isControllerConnected)
{
PC_Move = "WASD";
PC_Rotate = "Mouse";
PC_Item1 = "1";
PC_Item2 = "2";
PC_Item3 = "3";
PC_Item4 = "4";
PC_Inv = "I";
[ 15 ]
Interactive Input
PC_Pause = "Escape";
PC_AttackUse = "Left Mouse Button";
PC_Aim = "Right Mouse Button";
pcItem1 = KeyCode.Alpha1;
pcItem2 = KeyCode.Alpha2;
pcItem3 = KeyCode.Alpha3;
pcItem4 = KeyCode.Alpha4;
pcInv = KeyCode.I;
pcPause = KeyCode.Escape;
pcAttackUse = KeyCode.Mouse0;
pcAim = KeyCode.Mouse1;
}
else
{
PC_Move = "WASD";
PC_Rotate = "Mouse";
PC_Item1 = "1";
PC_Item2 = "2";
PC_Item3 = "3";
PC_Item4 = "4";
PC_Inv = "I";
PC_Pause = "Escape";
PC_AttackUse = "Left Mouse Button";
PC_Aim = "Right Mouse Button";
Xbox_Move = "Left Thumbstick";
Xbox_Rotate = "Right Thumbstick";
Xbox_Item1 = "D-Pad Up";
Xbox_Item2 = "D-Pad Down";
Xbox_Item3 = "D-Pad Left";
Xbox_Item4 = "D-Pad Right";
Xbox_Inv = "A Button";
Xbox_Pause = "Start Button";
Xbox_AttackUse = "Right Trigger";
Xbox_Aim = "Left Trigger";
pcItem1 = KeyCode.Alpha1;
pcItem2 = KeyCode.Alpha2;
pcItem3 = KeyCode.Alpha3;
pcItem4 = KeyCode.Alpha4;
pcInv = KeyCode.I;
pcPause = KeyCode.Escape;
pcAttackUse = KeyCode.Mouse0;
pcAim = KeyCode.Mouse1;
[ 16 ]
Chapter 1
xInv = KeyCode.I;
xPause = KeyCode.Escape;
}
}
Next, we will add a function to our script that will allow the player to switch between
control schemes:
void SwitchControlScheme(string Scheme)
{
switch(Scheme)
{
case "Scheme A":
SetDefaultValues();
break;
case "Scheme B":
if(!isControllerConnected)
{
PC_Move = "WASD";
PC_Rotate = "Mouse";
PC_Item1 = "Numpad 1";
PC_Item2 = "Numpad 2";
PC_Item3 = "Numpad 3";
PC_Item4 = "Numpad 4";
PC_Inv = "Numpad +";
PC_Pause = "Enter";
PC_AttackUse = "Right Mouse Button";
PC_Aim = "Left Mouse Button";
pcItem1 = KeyCode.Keypad1;
pcItem2 = KeyCode.Keypad2;
pcItem3 = KeyCode.Keypad3;
pcItem4 = KeyCode.Keypad4;
pcInv = KeyCode.KeypadPlus;
pcPause = KeyCode.Return;
pcAttackUse = KeyCode.Mouse1;
pcAim = KeyCode.Mouse0;
}
else
{
PC_Move = "WASD";
PC_Rotate = "Mouse";
PC_Item1 = "Numpad 1";
PC_Item2 = "Numpad 2";
PC_Item3 = "Numpad 3";
[ 17 ]
Interactive Input
PC_Item4 = "Numpad 4";
PC_Inv = "Numpad +";
PC_Pause = "Enter";
PC_AttackUse = "Right Mouse Button";
PC_Aim = "Left Mouse Button";
Xbox_Move = "Left Thumbstick";
Xbox_Rotate = "Right Thumbstick";
Xbox_Item1 = "D-Pad Up";
Xbox_Item2 = "D-Pad Down";
Xbox_Item3 = "D-Pad Left";
Xbox_Item4 = "D-Pad Right";
Xbox_Inv = "B Button";
Xbox_Pause = "Back Button";
Xbox_AttackUse = "Right Trigger";
Xbox_Aim = "Left Trigger";
pcItem1 = KeyCode.Keypad1;
pcItem2 = KeyCode.Keypad2;
pcItem3 = KeyCode.Keypad3;
pcItem4 = KeyCode.Keypad4;
pcInv = KeyCode.KeypadPlus;
pcPause = KeyCode.Return;
pcAttackUse = KeyCode.Mouse1;
pcAim = KeyCode.Mouse0;
xInv = KeyCode.JoystickButton1;
xPause = KeyCode.JoystickButton6;
}
break;
}
}
As you can see, this function is very similar to our SetDefaultValues() function;
it acts the same way. SwitchControlScheme() takes a string that determines which
control scheme to use and then assigns the appropriate data. The first scheme is
the default control scheme, while the other one is a new scheme. The new scheme
changes the following:
Pause has been changed to the Enter key and the Backspace key
[ 18 ]
Chapter 1
This button, when clicked, will call the SwitchControlScheme() function and pass it
a letter determining the control scheme being used.
= pcItem1;
= pcItem2;
= pcItem3;
= pcItem4;
pcInv;
= pcPause;
[ 19 ]
Interactive Input
Next, we are going to add a function that we'll call to switch our keys:
void SetNewKey(KeyCode KeyToSet, KeyCode SetTo)
{
switch(KeyToSet)
{
case KeyCode.Alpha1:
pcItem1 = SetTo;
PC_Item1 = SetString(pcItem1.ToString());
break;
case KeyCode.Alpha2:
pcItem2 = SetTo;
PC_Item2 = SetString(pcItem2.ToString());
break;
case KeyCode.Alpha3:
pcItem3 = SetTo;
PC_Item3 = SetString(pcItem3.ToString());
break;
case KeyCode.Alpha4:
pcItem4 = SetTo;
PC_Item4 = SetString(pcItem4.ToString());
break;
case KeyCode.I:
pcInv = SetTo;
PC_Inv = SetString(pcInv.ToString());
break;
case KeyCode.Escape:
pcPause = SetTo;
PC_Pause = SetString(pcPause.ToString());
break;
case KeyCode.JoystickButton1:
xInv = SetTo;
Xbox_Inv = SetString(xInv.ToString());
break;
case KeyCode.JoystickButton6:
xPause = SetTo;
Xbox_Pause = SetString(xPause.ToString());
break;
}
}
[ 20 ]
Chapter 1
This new function takes in two properties: the first one will be what KeyCode we set
and the second one will be what KeyCode we are setting the key to. You can see that
we also set our string variables, which are used in the GUI with another function.
We'll create that function now:
string SetString(string SetTo)
{
switch(SetTo)
{
case "Alpha1":
SetTo = "1";
break;
case "Alpha2":
SetTo = "2";
break;
case "Alpha3":
SetTo = "3";
break;
case "Alpha4":
SetTo = "4";
break;
case "Return":
SetTo = "Enter";
break;
case "Escape":
SetTo = "Escape";
break;
case "I":
SetTo = "I";
break;
case "JoystickButton6":
SetTo = "Start Button";
break;
case "JoystickButton1":
SetTo = "A Button";
break;
}
return SetTo;
}
[ 21 ]
Interactive Input
Now in our OnGUI function, we'll need to adjust some of our code. Before we
start our controls group, we will check whether our controls pop up is activated.
Add the if statement to our code and encapsulate the Controls Group:
if(!ShowPopup)
{
Next, we'll edit some of our GUI buttons to allow customization. Start with the
PC_Item1 button and change it to this code:
if(GUI.Button(new Rect(150, 125, 135, 20), PC_Item1))
{
ShowPopup = true;
PreviousKey = pcItem1;
}
PC_Item2
PC_Item3
PC_Item4
PC_Pause
PC_Inv
Xbox_Inv
Xbox_Pause
Chapter 1
SetNewKey(PreviousKey, orig_pcItem1);
ShowPopup = false;
}
if(GUI.Button(new Rect(150, 150, 135, 20),
{
SetNewKey(PreviousKey, orig_pcItem2);
ShowPopup = false;
}
if(GUI.Button(new Rect(150, 175, 135, 20),
{
SetNewKey(PreviousKey, orig_pcItem3);
ShowPopup = false;
}
if(GUI.Button(new Rect(150, 200, 135, 20),
{
SetNewKey(PreviousKey, orig_pcItem4);
ShowPopup = false;
}
if(GUI.Button(new Rect(150, 225, 135, 20),
{
SetNewKey(PreviousKey, orig_pcInv);
ShowPopup = false;
}
if(GUI.Button(new Rect(150, 250, 135, 20),
{
SetNewKey(PreviousKey, orig_pcPause);
ShowPopup = false;
}
if(GUI.Button(new Rect(325, 225, 135, 20),
{
SetNewKey(PreviousKey, orig_xInv);
ShowPopup = false;
}
if(GUI.Button(new Rect(325, 250, 135, 20),
{
SetNewKey(PreviousKey, orig_xPause);
ShowPopup = false;
}
GUI.EndGroup();
}
[ 23 ]
"2"))
"3"))
"4"))
"I"))
"Escape"))
"A Button"))
"Start Button"))
Interactive Input
When the player clicks on one of these new buttons, the SetNewKey function
is called. When called, we pass PreviousKey, which is the key the player is
customizing, as well as the key they select, which is the new value of PreviousKey.
This is a great and simple way to change controls, which makes it simple for
the player.
Here, we call our SetDefaultValues() function, and then reset some other
variables. Resetting the ShowPopup Boolean and our PreviousKey KeyCode
will ensure that everything related to customization of controls has been reset.
[ 24 ]
Chapter 1
When the player clicks on this button, the controls will be set to their default values.
Here is the finished product of the script that you just created:
Playtesting
Now for the most important part, playtesting! Go ahead and play with the GUI buttons
and make sure everything works as it is supposed to. Add the Debug.Log statements
to where you think you may have problems and see which variable is set to what.
Plug in your Xbox 360 Controller and make sure that it detects your controller.
Downloading the example code
You can download the example code files from your account at
http://www.packtpub.com for all the Packt Publishing books
you have purchased. If you purchased this book elsewhere, you can
visit http://www.packtpub.com/support and register to have
the files e-mailed directly to you.
[ 25 ]
Interactive Input
Summary
In this chapter, you learned a great deal about how to create and handle control
schemes. You also learned how to change preset control schemes, as well as swap
the controls. You can now create a game that will support both the keyboard/mouse
and Xbox 360 Controller. This is something that is pivotal to games and game design;
not everyone likes to use non-customizable controls.
In the next chapter, we'll go over how to utilize the GUI more in depth. You will
learn about how to create 3D GUI elements as well as 2D elements. Later on in this
book, we will use these new GUI elements to create a more engaging experience.
[ 26 ]
www.PacktPub.com
Stay Connected: