O'Reilly logo

Unity Game Development Scripting by Kyle D'Aoust

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required