Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

Table of Contents (17 chapters)
Unity Game Development Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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...