Bullet Penetration

A introduction and background into bullet penetration system in Wilderness.

Posted by on

### Introduction

Bullet penetration is something that is very common in today's AAA games. However, it isn't as common in Indie games as it should be. Settings up a basic system, like the one we use, took about a week and a half, but the result is another layer of immersion and realism. The idea of having bullet penetration may seem like a very complicated subject, having to take many values into account, such as bullet velocity and damage, but thanks to Unity, it can be a lot easier than first anticipated. For our approach, we decided to use raycasting but there are other approaches, such as particle systems.

As this picture shows, the bullet has declining damage and velocity, which is indicated in the colour strength (Green being high velocity/damage, Red being low velocity/damage). As the bullet passes through each material, the velocity is reduced depending on the material. Here is a comparison of a 9x19mm bullet vs. a 5.56x45mm bullet.

9x19mm Parabellum bullet:

5.56x45mm NATO bullet:

The 9mm bullet fails to punch through two layers of wood, but the 5.56mm bullet powers on through more than 5 layers.

### How we did it:

Lets get right into it. Before programming a solution, we needed to set up tags. We already set up a few named "Wood", "Concrete", "Water", ect. for differentiating our footsteps on different materials. However, we want more precise control over material penetration, so we set up a few more tags named "WoodThin", "WoodThick", "ConcreteThin", "ConcreteThick", "MetalThin", "MetalThick" so we could have different penetration values for the same material. We originally looked into calculating the mesh width at that particular angle, but it wasn't practical for runtime. However, Unity has very handy function named Physics.RaycastAll. This sends a ray through all objects for a specified range, logging each collider hit.

``````Ray ray = new Ray(cam.position, direction);
RaycastHit[] hits = Physics.RaycastAll(ray, 1000.0f);``````

This returns an array of RaycastHits, a struct that contains information about the hit point and collider that was hit. We can then loop through each of the hits and check information from them.

``````foreach (RaycastHit hit in hits)
{
if (hit.collider.tag.ToLower() == "dirt")
{
// Do stuff here
}
}``````

This is an easy way to loop through the hits and check information. However, we need to record the bullet's current velocity and damage. We just do this via two floats, currentDamage and currentVelocity.

``````float currentVelocity = 1f;
float currentDamge = 1f;``````

To simply things, we made a data class named BulletPenetrationStats. This class has a simple constructor, taking in the tag name as a string, bullet damage reduction value, bullet force reduction value and a minimum velocity to penetrate value.

``````private class BulletPenetrationStats
{
public string m_sTag;
public float m_fDamageReduction;
public float m_fVelocityReduction;
public float m_fMinimumPenetrateForce;

public BulletPenetrationStats (string tag, float damageReduction, float velocityReduction, float minPenetrationForce)
{
m_sTag = tag;
m_fDamageReduction = damageReduction;
m_fVelocityReduction = velocityReduction;
m_fMinimumPenetrateForce = minPenetrationForce;
}
}``````

Earlier in our weapon script, we can declare a private array of variables for each kind of bullet we want, like 9mm High Velocity or 9mm Armour Piercing. For the moment, we just have 9x19 and 5.56x45 rounds balanced and in-game.

``````private BulletPenetrationStats[] nineMilimetreRoundStats = new BulletPenetrationStats {
new BulletPenetrationStats ("WoodThin", 0.2f, 0.1f, 0f),
new BulletPenetrationStats ("Wood", 0.4f, 0.4f, 0.2f),
new BulletPenetrationStats ("WoodThick", 0.6f, 0.7f, 0.3f),
new BulletPenetrationStats ("ConcreteThick", 1f, 1f, 1.1f),
}``````

These aren't representative of the real stats, but a quick example of how we set up the data.

Then, we check at the end of our foreach bracket, looping through the stats and taking away the velocity and damage values. Finally, we check that they are above zero, otherwise we return out of our Shoot function.

``````BulletPenetrationStats currentStats;
foreach (BulletPenetrationStats entry in nineMilimetreStats)
{
if (entry.m_sTag.ToLower() == hit.collider.tag.ToLower()
{
currentDamage -= entry.m_fDamageReduction;
currentVelocity -= entry.m_fVelocityReduction;
currentStats = entry;
break;
}
}
if (currentVelocity <= 0 || currentDamage <= 0 || currentVelocity <= currentStats.m_fMinimumPenetrateForce)
{
return;
}``````

However, I'm very pedantic about little details. I wanted bullet holes on both sides of the object, so that you could visually trace the path of the bullet. As this foreach statement is run in a single frame, every object is static, so we don't need to worry about accidentally hitting a different object. A single Physics.Raycast will suffice, saving much needed performance on the server side. To calculate the Raycast angle, we just need to take the last hit point away from the current hit point, to get the exact angle needed for the bullet hole on the other side.

``````Vector3 startPos = hit.point;
Vector3 endPos = lastHitPoint;
Vector3 dir = startPos - endPos;
Ray ray = new Ray(transform.position, dir);
if (Physics.Raycast(ray, 1000f)) {
// Check surface and instantiate bullet hole
}``````

### Conclusion

And that is a very brief and simple introduction to bullet penetration. It will definitely be taken further and expanded, but this is the depth and complexity required at the moment. One way to expand it would to have a custom shader that has different penetration values, so you could adjust them for each material, but that's an idea for the future. Thanks very much to IRetrograde, who explains it in much more depth than I could. A very handy article on bullet penetration by him can be found here.