#14 | 2023-07-14 | Overhauling Code


Hello!

Through my attempts to create an acid pit / barrel, I've learned that my player controller scripts interfere with any code I write for the acid pit.

My goal was to make it so when the player collides with any object with the "Acid" tag, their Rigidbody2D's Gravity Scale changes to make them fall slower, making it look like their sinking into the pit before they are killed.

In order to facilitate this, I've had to completely overhaul my player controllers and how my player handles collision, death and respawning.



I'm going to explain what I did as best as I can.

First, I created a new script titled "PlayerCollisionDetection". This script handles collision with the floor, changing numerous booleans and setting the speed and gravity scale of the player.



Next, I rewrote my player controllers. I removed all code relating to collision and made a reference to the collision detection script. If the booleans "movementEnabled" and "jumpEnabled" in that script are true, the respective functions are enabled in the player controllers.



I then created another script, "PlayerDeath". This script handles killing the player. Making another reference to the collision detection script, I created a function called "DeathCheck". If the booleans "collidingWithTrap" or "collidingWithAcid" , from the collision detection script, are equal to true, their respective coroutines are called. Those coroutines handle changing a boolean, "playerDied" from the respawn manager script, spawning a death effect and destroying the player prefab.



Lastly, I worked on the Respawn Manager which handles spawning the player when the game starts and respawning the player when they die. If "playerDied" is equal to true, the player is respawned and the boolean is set to false to prevent infinite spawning.



The biggest issue I had when overhauling my code was getting the player to respawn after the died.


It took quite some time to figure out, but was very rewarding when I got it to work.


I'm very, very proud of my work so far. All of this code I've written from scratch, not taken from any tutorials. The more I build games the more I learn, and the more I have fun!

Special thanks to "code-garden", "KrakyKrak" and "NightDemon" for their attempts to help me with issues that arose.


Thanks for reading and stay tuned for the next log!

Get Factory of the Damned

Leave a comment

Log in with itch.io to leave a comment.