Man… today was a little tougher than I thought it was going to be. The goal was to create a powerup that would add a life to my Player. Simple idea, but a little tricky when it came to the finer details.
First on the list was to create a new sprite image and an animation to go with it. I spent a little bit of time in Photoshop and finalized a sprite for a plus one life powerup. I then created a game object and whipped up a little animation that flashed between a few colors and scaled up and down.
After the animation, I coded the commands for collecting the powerup and adding one life back to the Player. It was important to also communicate with the User Interface (UI) to let the player know that they have gained one life when collecting the powerup. Now that everything was working the problems began…
The visualization for Player damage was not recognizing how many lives the Player had remaining. Once damaged and a life was gained, the Player would take damage on the wrong side of the object or no visual damage would be taken. The fact that I decided to make my damage appear randomly on the left or right side of the Player object was really throwing a wrench in my code. I finally resorted to working my way through the code one “if” statement at a time and ensured the code was being read by the use of a few “Debug.Log” statements. The below code shows how I finally solved the issue:
The first code I wrote for gaining a life was actually extremely similar to this. I think that’s what made it so confusing. I was only off by misplacing a couple lines of code and that small mistake completely broke my whole life powerup. Coding is a wild experience but I am stoked I was able to find the solution!
See ya tomorrow!