Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity Game Development Scripting

You're reading from   Unity Game Development Scripting Write efficient C# scripts to create modular key game elements that are usable for any kind of Unity project

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781783553631
Length 202 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Kyle D'Aoust Kyle D'Aoust
Author Profile Icon Kyle D'Aoust
Kyle D'Aoust
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Interactive Input FREE CHAPTER 2. GUI Time 3. Expandable Item Classes 4. Inventory 5. Enemy and Friendly AIs 6. Keeping Score 7. Creating Save and Load Systems 8. Aural Integration 9. Game Settings 10. Putting It All Together Index

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.

Swapping control schemes

Our first method will be to switch between preset control schemes. To start off, we'll add a bunch of variables that we will use for our controls:

string ControlScheme;
public KeyCode pcItem1, pcItem2, pcItem3, pcItem4, pcInv, pcPause, pcAttackUse, pcAim, xInv, xPause;

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";
    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;
    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";
      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:

  • Item keys are now on the keypad
  • Inventory buttons are now the + key and B key
  • Attack/use inputs are switched on the mouse
  • Pause has been changed to the Enter key and the Backspace key

Adding a control switch button to the GUI

Lastly, we'll need to add a GUI button to our OnGUI function to allow the player to switch control schemes. Add the following before the line that ends the group:

GUI.Label(new Rect(15, 345, 125, 20), "Current Control Scheme");
if(GUI.Button(new Rect(25, 370, 135, 20), ControlScheme))
{
  if(ControlScheme == "Scheme A")
  {
    SwitchControlScheme("B");
    ControlScheme = "Scheme B";
  }
  else
  {
    SwitchControlScheme("A");
    ControlScheme = "Scheme A";
  }
}

This button, when clicked, will call the SwitchControlScheme() function and pass it a letter determining the control scheme being used.

Cycling control inputs

Our next method of customization will let the player click on one of the GUI buttons in our controls, and pick another control to switch it. To start off, we'll add variables that we'll use to hold the original values of our controls. The last two variables will be used to allow us to customize our controls:

private KeyCode orig_pcItem1, orig_pcItem2, orig_pcItem3, orig_pcItem4, orig_pcInv, orig_pcPause, orig_xInv, orig_xPause;
bool ShowPopup = false;
KeyCode PreviousKey;

In the SetDefaultValues function, assign these variables to our previous control variables in both the if and else statements:

orig_pcItem1 = pcItem1;
orig_pcItem2 = pcItem2;
orig_pcItem3 = pcItem3;
orig_pcItem4 = pcItem4;
orig_pcInv = pcInv;
orig_pcPause = pcPause;

Assign the Xbox 360 Controller controls in the else statement:

orig_xInv = xInv;
orig_xPause = xPause;

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;
  }
}

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;
}

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;
}

Do the same thing for the following buttons:

  • PC_Item2
  • PC_Item3
  • PC_Item4
  • PC_Pause
  • PC_Inv
  • Xbox_Inv
  • Xbox_Pause

Set ShowPopup to true and PreviousKey to its expected value, accordingly, such as pcItem2, pcItem3, pcItem4, and so on. Place a closing bracket afterwards to close the if statement that we created earlier.

Adding the controls pop up to the GUI

It's time to add our controls pop up to the GUI. This is where the player will select what control to swap. To do this, we will add an else statement, extending our if statement, to create the pop up:

else
{
  GUI.BeginGroup(new Rect(Screen.width/2 - 300, Screen.height / 2 - 300, 600, 400));
  GUI.Box(new Rect(0,0,600,400), "Pick A Control to Switch");
  if(GUI.Button(new Rect(150, 125, 135, 20), "1"))
  {
    SetNewKey(PreviousKey, orig_pcItem1);
    ShowPopup = false;
  }
  if(GUI.Button(new Rect(150, 150, 135, 20), "2"))
  {
    SetNewKey(PreviousKey, orig_pcItem2);
    ShowPopup = false;
  }
  if(GUI.Button(new Rect(150, 175, 135, 20), "3"))
  {
    SetNewKey(PreviousKey, orig_pcItem3);
    ShowPopup = false;
  }
  if(GUI.Button(new Rect(150, 200, 135, 20), "4"))
  {
    SetNewKey(PreviousKey, orig_pcItem4);
    ShowPopup = false;
  }
  if(GUI.Button(new Rect(150, 225, 135, 20), "I"))
  {
    SetNewKey(PreviousKey, orig_pcInv);
    ShowPopup = false;
  }
  if(GUI.Button(new Rect(150, 250, 135, 20), "Escape"))
  {
    SetNewKey(PreviousKey, orig_pcPause);
    ShowPopup = false;
  }
  if(GUI.Button(new Rect(325, 225, 135, 20), "A Button"))
  {
    SetNewKey(PreviousKey, orig_xInv);
    ShowPopup = false;
  }
  if(GUI.Button(new Rect(325, 250, 135, 20), "Start Button"))
  {
    SetNewKey(PreviousKey, orig_xPause);
    ShowPopup = false;
  }
  GUI.EndGroup();
}

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.

Resetting the controls

In this section, we will add the ability to allow the player to reset the controls to their default values.

Adding the Reset function

The reset function will use our SetDefaultValues() function as well as reset a couple of our other variables:

void Reset()
{
  SetDefaultValues();
  ShowPopup = false;
  PreviousKey = KeyCode.None;
}

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.

Adding the Reset input

Now, we'll make a GUI button that will call the Reset function. Add this just before the line of code that ends the GUI group in the OnGUI() function's if statement:

if(GUI.Button(new Rect(230, 370, 135, 20), "Reset Controls"))
{
  Reset();
}

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:

Adding the Reset input
You have been reading a chapter from
Unity Game Development Scripting
Published in: Dec 2014
Publisher:
ISBN-13: 9781783553631
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime