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

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.

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 $19.99/month. Cancel anytime