Object Pooling

Object Pooling

Soon after the first couple of levels were done, it became very obvious that there were some performance issues. One obvious optimization to make was pooling the projectiles instead of constantly instantiating and destroying them. Setting the pool up was easy enough; set up a pool singleton that is generic enough to take any kind of gameobject (this would come in handy later) and building some methods to take objects in and out of the pool. There were some bugs around properly activating and deactivating objects but otherwise it was up and running very quickly.

Once I had the projectiles pooling, I added another pool to handle the audio source, both for the firing sound and the collision impact sound. In later iterations, I added a separate pool for the different VFX required, such as muzzle flash, impact effects and so on. The VFX pool is actually handled by a pool object from a VFX asset package. The Asset Store saves the day again (or at least saves me a ton of time).

Rigid Bodies and Momentum

There was an insidious bug that took a while to figure out. As soon as I started pooling the projectiles, I found that some of them wouldn’t fire properly. Some would be way too fast, some would fire in a weird and unexpected angle. With some poking, I figured out it had to be the momentum of the projectile transferring from the projectile’s last state to the initialization coming out of the pool. For quite a while, I tried fixing this at deactivation time. The projectiles are pooled once they hit a surface so that seemed like the best place to kill the momentum and reset the projectile. At this point, I’m not 100% sure what the problem was but the projectiles were either being pooled before they were fully reset or the firing script was getting them out of the pool before the reset took hold. In either case, the solution was staring at me in the face, resetting the projectiles during the instantiation method. Instead of trying to return them to the pool with no momentum, it was better to kill the momentum right before adding the “firing” momentum. Once I reset the momentum right before firing the projectile (within the firing method), the weird projectile angles went away.

Other progress

By this time, the game has a couple of levels, a few materials, including some translucent glass. The game also has a few sound effects and some background music, too. The text is fixed to the player position and the score has a nicer text style. There’s some fun debug text telling you what the state of the game is. Eventually, that text will be replaced with more impactful messaging but this gets the state of the game across for now (just so that people don’t get lost at the end of the level and such).

Posted in VR Development and tagged , , .