Adding Some Collectible Items! (2.5D Platformer)
I recently added some collectible items to this 2.5D platformer I’ve been working on for the GameDevHQ course. I’ve done something a bit like this previously, but essentially I’ve defined an enum in a static script (‘VariableContainer’) which specifies what types of collectibles are available. Each instance of a Collectible script has a collectible type assigned to it, and whenever the player triggers it (via OnTriggerEnter) this enum variable is broadcasted to the rest of the game via a custom UnityEvent.
Among the scripts which listen for this event are a PlayerInventory script, a UIManager for UI feedback, and an AudioManager for audio feedback. Each of these have a method with a switch statement which checks to see what exactly has been collected, allowing them to behave differently accordingly.
As such, the player’s inventory is updated and the display also updates accordingly. I haven’t actually set the audio part of this up just yet, but I’d be aiming to have different audio feedback for each of the collectibles and, ideally, a randomised piece of audio (from a list of AudioClips) to go with each coffee cup collected. I also ended up deciding each coffee cup increases the player’s speed by 1, which livens things up at the risk of making things a tad less stable.
Aside from this, I’ve also set the collectibles up to bob up and down as well as rotate on their Y axis (just to help them come alive a bit more). These both work using Vector3.Lerp and Mathf.PingPong to transition between a max and min height and rotation. I’ve also implemented some randomisation to their movement speed using Random.Range, setting a minimum and maximum value of 1 and 2 respectively.
I’ll include the code as it currently stands below. Happy coding! 😉