Let's get set and show them
The next step in our Control
script is to set up our controls to be able to customize what they do, and display what each control does.
Adding variables for each control
At the top of your script, after your other variables, add these new variables:
public string PC_Move, PC_Rotate, PC_Item1, PC_Item2, PC_Item3, PC_Item4, PC_Inv, PC_Pause, PC_AttackUse, PC_Aim; public string Xbox_Move, Xbox_Rotate, Xbox_Item1, Xbox_Item2, Xbox_Item3, Xbox_Item4, Xbox_Inv, Xbox_Pause, Xbox_AttackUse, Xbox_Aim;
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"; 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.
Adding a function to display the variables
Next, we will use the OnGUI
function to display our controls onto the screen. We will create a menu that will show each action and their controls for a PC and Xbox 360 Controller, very similar to the table shown at the beginning of this chapter. Add this code to the bottom of your script:
void OnGUI() { GUI.BeginGroup(new Rect(Screen.width/2 - 300, Screen.height / 2 - 300, 600, 400)); GUI.Box(new Rect(0,0,600,400), "Controls"); GUI.Label(new Rect(205, 40, 20, 20), "PC"); GUI.Label(new Rect(340, 40, 125, 20), "Xbox 360 Controller"); GUI.Label(new Rect(25, 75, 125, 20), "Movement: "); GUI.Button(new Rect(150, 75, 135, 20), PC_Move); GUI.Button(new Rect(325, 75, 135, 20), Xbox_Move); GUI.Label(new Rect(25, 100, 125, 20), "Rotation: "); GUI.Button(new Rect(150, 100, 135, 20), PC_Rotate); GUI.Button(new Rect(325, 100, 135, 20), Xbox_Rotate); GUI.Label(new Rect(25, 125, 125, 20), "Item 1: "); GUI.Button(new Rect(150, 125, 135, 20), PC_Item1); GUI.Button(new Rect(325, 125, 135, 20), Xbox_Item1); 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.