This is one of those things that has many tutorials for but all the ones I found felt too complicated to me. Of course it is possible to simulate all the details of real life, but if it does not lead to a result that a player can predict I don’t see the purpose for extra complication!
So, here I tried to make a very simple setup that mimics a bullet penetration system like you find in TheHunter or Sniper Elite. Basically, we want to know if the caliber of bullet penetrates far enough to hit specific organs.
-
Adding on to the first person template… Use a MULTI-Linetrace
-
and a simple switcher to test different calibers
-
After the line trace, we will send the hit data to a function which will determine what components were hit AND calculate if they were within the bullet calibers penetration range.
-
Overview of the function:
- We are going to output what components were hit (representing organs or body parts) as an array of names.
- We will send this array through an interface message to the hit actor
- for the first index, cache the location because the initial hit is the location we will measure distance from for subsequent hits
- each index after the first will compare it’s location with the first hit hits location (the entry point) to get the distance. Compare the distance to the current calibers penetration distance to determine if it hit the component.
- If the component was hit, we grab it’s tag and add to the Name array.
-
Comparing entry location to the point where we hit a component, is the distance less than the bullet calibers penetration distance?
-
Select node for each caliber
-
Get tag from component (only one tag so I can just get first index), add to an array. Always be sure to clear your array at the beginning of the function so that you aren’t holding onto old stuff you don’t need anymore.
-
When the loop completes, return the hit actor (so interface knows who to call) and the newly created array.
IMPORTANT! Notice that I set the hit actor as a variable. This is important because the multi-hit line trace will go all the through and hit any other actor. If you didn’t set this variable, the last hit might be set to some random actor, and then your interface call would fail (or at least not go where you wanted it to.) -
Over on the actor getting shot, notice a few things -
-
we are using static mesh components, but you can just set the collision to something that won’t interfere with anything else.
-
Each component has a tag added - this would be the name of the body part/organ that you want.
- From the interface event you can do whatever you like. Good to use a print string to make sure everything is working correctly though. You should get a list of the components that were hit. Try setting some bullet caliber penetrations to different values to determine how far you need to penetrate to hit your heart from side, from rear, etc.
I’ve set my penetration variables so that a 22 bullet won’t reach the mooses heart, but a 30-30 will (from broadside, but not all the way from behind). And 45-70 will go through heart and lung from any angle. This should be an easy system to work with because just the scale of the animal will determine what caliber bullets are appropriate for it. Therefore as long as scale is close-ish to real world, we should have close-enough realism for a game without having to do tweak a bunch of different values for various situations.
That’s it! This is a very simple setup that is easy to understand, but it can give a sense of depth and realism for the player. With the knowledge of what bodypart/organ was hit, and what caliber bullet was involved, you have all the information you need to build an expansive damage/wound system if you wanted.
If you wanted to know how many times the target was shot, you can just get the incoming array on the hit actor and append to another, that way you keep a running tally on a per actor basis.
If you are confused by some of the stuff using arrays and tags and components - don’t worry. Just google the term along with Unreal or UE4 and you’ll find tons and tons of resources. It is all simple stuff. Just try to make some basic examples that you can first explain in plain language before you write any code, and then you’ll get it in no time.
Helpful resource for arrays, maps, list:
(disregard the 1 star review, this is the best resource you got for the subject)
Also from epic, grab the Using Enumerations in Blueprints and Blueprints. And of course don’t forget the Content Examples project.:
You’ll want to get familiar with these data types and using interfaces and event dispatchers ASAP. It will help you avoid brain-twister code and graphs that make your eyes hurt.