Adding in-game items and power-ups
Next, you will learn how to alter the player's attributes by creating in-game items that can be picked up by the player. Let's create the prefab in the Unity Editor before proceeding to the script. I did a simple cross-shape prefab using boxes, removed the colliders from the boxes, and attached a sphere collider component to it, which are marked as a trigger. I also set its tag as Item
so that we can use it later. The tag is a very useful functionality in Unity, which allows you to group game objects into the same category for your scripts to identify them during gameplay. For example, you might define Player
and Enemy
tags for player-controlled characters and non-player characters respectively.
Instead of creating an individual script for every item of different purpose, we can create one universal power-up script that alters different player attributes depending on how we set the variables.
Therefore, the first thing we need to add to the power-up script...