Battery collection with triggers

To trigger the differing states of our Battery GUI, we will use a function called OnTriggerEnter() to detect interaction with objects that have trigger mode colliders, that is, our collectable batteries.

Before we add this function to our PlayerCollisions script, we will add a public member variable at the top to hold an audio clip to be played when the player picks up a battery as a form of audio-based feedback to back up the GUI element.

Open the PlayerCollisions script by double-clicking on its icon in the Scripts folder of the Project panel. This will launch the script in the script editor, or simply switch back to it for you, if you already have the file opened. Add a public member variable for the audio clip ...

Get Unity Game Development Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.