How It Works

VariaBULLET2D handles collisions through the reserved layers, their interactions via the collision matrix and scripts that contain OnCollisionEnter2D and OnLaserCollision event methods.

Collision detection in return determines the destruction of bullets (or repooling), applying damage to objects that are hit, and handling audio/visual cue such as instantiating explosions and flickering damageable objects.

This section of the guide is meant to better explain how this all connects to form a single system.

Sorting

Shots inherit their sprite sorting layer though their emitter sorting layers, while emitters receive this layer info through the controller via the dropdown Sort Layer selection.

Recalling that Unity uses a reverse layering system (layers on top actually appear behind layers below them), you’ll note that an ND_PlayerBullet appears under ND_EnemyBullet and so forth.


Sorting Order

Further sorting within the layer is determined automatically (typically in order of emitter creation). However sorting order can be changed at the emitter level by changing Order in Layer of the Sprite Renderer directly.


Physics

Shot bullets assign their Physics Layer to the same name as their Sorting Layer and these collisions are managed in the Layer Collision Matrix previously described.


Collision Events

If a shot collision is detected by the underlying matrix (whenever it intersects with an Enemy or Terrain, etc) it fires the OnCollisionEnter2D (in case of a bullet collision) or OnLaserCollision (in case of a laser collision) event methods that are defined in the collision script attached to the object it collided with. Bullets also carry their own OnCollisionEnter2D event method in their attached Shots scripts that fire upon collision.

For a bullet, the OnCollisionEnter2D method in its Shot script (via the ShotBase class) destroys, or repools, the bullet and (via the ShotBullet class) instantiates an explosion particular to that bullet.

For the object that a bullet collides with, the OnCollisionEnter2D/OnLaserCollisions method in its attached ShotCollision script instantiates explosions particular to those objects (more on explosions in the next section).


Shot Collision Damage Script

In the case of a ShotCollisionDamage script (usually attached to player/enemy/destroyable objects), the script also deals damage by decrementing [A] HP. The script also flickers the object to show damage via [B] Damage Flicker, [C] Flicker Duration and [D] Damage Color and destroys it if HP reaches 0.


The amount of HP damage a shot creates is set by the incoming shot script’s [E] Damage Per Hit and, in the case of Laser shots, [F] Hits Per Second.