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

Creating player interactions


Here, we will create ways for the player to interact with the game world. For our game, we will have the player shooting their gun, collecting potions, and pausing the game as interactions. Create a new C# script and name it PlayerInteraction. First, we will create a couple of variables and add them to our script:

public GameObject Projectile, Potion;

The Projectile GameObject will be the bullets that we shoot and the Potion GameObject will be the potion prefab that we created earlier.

Shooting and pausing

We will create the functionality to shoot the gun and pause the game. Add this Update function to your script:

void Update ()
{
if(Time.tmeScale != 0.00f)
{
  if(Input.GetButtonUp("Fire1"))
    Instantiate(Projectile, transform.position, transform.rotation);

  if(Input.GetButtonUp("Esc_Key"))
  {
    if(Time.timeScale != 0.00f)
      Time.timeScale = 0.00f;
    else
      Time.timeScale = 1.00f;
  }
}
}

The first if statement will allow the player to shoot the projectile...

lock icon The rest of the chapter is locked
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 £16.99/month. Cancel anytime