Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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 switch!

Now, we'll create a way for the player to switch between PC and Xbox 360 Controller controls.

Creating control profiles

To create our profiles, we'll need to add a new variable. Add the following enum to the top of our script, before the class declaration:

public enum ControlProfile { PC, Controller };

Add it to your variables as well, like this:

public ControlProfile cProfile;

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 are setting our enum variable in the DetectController() function to give us a default control profile. This is a fast and effective way to give our player the best control profile possible.

Adding a profile switching function

Next, we'll add a function that we can call to manually switch the control profile. Add this function to our code:

void SwitchProfile (ControlProfile Switcher)
{
  cProfile = Switcher;
}

We can call this function later to let the player choose between using the keyboard/mouse or the Xbox 360 Controller.

Adding the GUI interaction function

Now, we'll add a button to the bottom right of our controls page to let the player pick between the keyboard/mouse and Xbox 360 Controller. Add this code to your onGUI() function, just before the line where we end the group:

GUI.Label(new Rect(450, 345, 125, 20), "Current Controls");
if(GUI.Button(new Rect(425, 370, 135, 20), cProfile.ToString()))
{
  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.

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