Multi-Pierce Raycast Shot.

What I’m asking about is how to make a non-projectile shot pierce through multiple objects. Preferably in C#. However, I also am looking for a way to govern the pierce, due to the long range of the weapon I’m setting this on, infinite pierce would be far too powerful.

I have a basic shooting script (based highly off of the Intro to Shooter Mechanics tutorial example, with small, mostly immaterial changes), and I am trying to extend it a bit by using this. But, I am fine with just dropping the one I’m currently using, and writing a new one if needed.


float maxPierceDistance = 100f;
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit blockHit;
if(Physics.Raycast(ray, out blockHit, maxPierceDistance, LayerMask.GetMask(“Cover”)))
maxPierceDistance = blockHit.distance;
RaycastHit hit = Physics.RaycastAll(ray, maxPierceDistance, LayerMask.GetMask(“Enemy”));