Sunday, March 13, 2016

It ain't cheatin' if it works...

So, I was bug stomping in Space Battles the other day, and I realized that I was wasting time and effort.  The problem I was working on was with our respawning.  After being killed, ships were originally set to just immediately jump to a new, random location and update the score.  But, this looks a little odd when the explosion of the ship is still going on and the ship has reappeared already.  I also had some issues where I wasn't resetting the shields correctly.  You could fly into the planet/star, die, and reappear with your shields at the exact same strength they were before you death-by-planet'ed.  D'oh.  You can see the problems in this gif.  (I messed with the frame rate and added some key frames to exaggerate the spots where the problems happen.  Also, some of the art is placeholder stuff...)

So, I was working at putting in a little delay and resetting the shield parameters to their starting values, but I was having problems.  I got the shields fixed and I figured out to move my respawning code into a separate function from where I turned off the Sprite Renderer component of the ship, so I could call the respawner as a "coroutine."  Coroutines are simply functions that make use of the "yield" keyword.  Basically, when the script hits the "yield" keyword, it pauses executing that script and returns to the rest of whatever function called it for the rest of that update.  You can then use WaitForSeconds(waitTime) to cause the coroutine to wait for waitTime seconds until it resumes execution.

But, my ships engine particle systems were still playing after the Sprite Renderer was disabled.  Of course, this makes total sense because the particle systems are set up as separate child game objects of the parent ship.  Now, I spent a good long while and many attempts to turn off the particle systems when the ship was killed.  For whatever reason, the my initial guesses were coming up empty.

Finally, I realized it wasn't cheating if it worked.  It might be silly that I couldn't quickly and easily turn off the particle systems, but I didn't need to.  All I needed to do was turn off the player controller script and move the ship off the screen the instant it was killed.  Then, it wouldn't be able to shoot or fire its engines until it was moved to its new spawn point and its player controller was turned back on.

I could also get rid of the annoying velocity carryover (see how the ships keep moving the same direction they were before they respawned in the gif) by setting the Rigidbody2D.drag variable to a really high number like a billion (fine, 1000 in actuality) while the ship was out of sight.

By the way, I'm fairly certain that I couldn't solve the problem by disabling the entire ship game object because I think that would stop the execution of the coroutine that was called by a component of the ship.  I might be wrong though, I've only been doing this for a month or so...

I'll put together a gif of the new respawning and update this post when I get the chance.  By the way, the white bars on the top and bottom of the gif are from Sketchbook Pro 7 where I used the flipbook feature to create my gif.  Didn't know I could do that until I tried it.  Fun times.

1 comment: