Train Defense 2‎ > ‎

Bullets, Damage, Fragments and Sound

posted Jul 20, 2012, 9:14 AM by James Podesta   [ updated Jul 22, 2012, 5:01 AM ]
Fair bit of progress to report..

Firstly, I implemented the projectile system so my guns can fire. Nice and extensible scripts, as one would expect from Unity. A projectile script manages a list of bullets and builds them into a dynamic mesh each frame. Everything dynamic has to be indexed triangle lists, instead of strips as I had in the old code. Not much I can do about this in Unity so I just have to pay the cost.  A series of bitflags determine what enemies a turret targets, and what collision objects a bullet can hit.

A Health script is added to all destructable objects. I've added armour to objects in Train Defense 2. Not sure how I overlooked that in the original Train Defense - I had just made enemies with bigger hitpoints. Now I have a choice of more hitpoints, or more armour, which opens a lot of strategic elements. So whenever a bullet hits a 'PushOutObject' or 'poo', it calls an appropriate TakePhysicalDamage or TakeEnergyDamage, etc, method on the script (which I keep a handy reference to in the poo).

I'm trying to avoid doing GetComponent<> calls during Update() for performance, so currently the PushOutObject is serving as a bit of a cache for common script types, like Health and DynamicModel.  Not sure if it will stay this way. I think the 'cached references' could probably be its own object to keep the collision object simple and clean... something I'll play with later anyway.

The fragment system is now in place - identical to the old Train Defense fragments. Since each model is made of primitives (spheres, boxes, cones), I create meshes for each of these individual elements as well as the single mesh. When something dies it spawns a bunch of fragments from a pool and positions and assigns the appropriate mesh to each meshFilter. They bounce around for a bit and shrink out of existance.

Finally I've added a SoundManager. Seems that you can't avoid adding your own soundmanager to unity if you want any decent control over sounds. I've added a simple SoundManager script that lets me play from a pool of channels, and control how many sounds of a particular group can play at once. So, for instance, I can set that only 4 bullet type sounds will play at once. This is important when you have 40+ enemies all machine gunning at once. You can't just let every bullet fired play a sound. I've chucked in bullets and explosion sounds for now. I don't want to put too many in till the last moment since it will make the Unity web download a lot larger.

So far its all running at 60fps solid on my iPad3 at Retina resolution, so I'm pretty confident this should all work out. Apart from particles and passengers, theres no majorly expensive systems left to add.

Train Defense 2 - Prototype 006