[Tutorial] Implement locational damage (headshots)

I wanted to implement locational damage, and it was a very frustrating experience to look for the relevant information to implement such a common thing, so here’s a mini tutorial to implement it in the SimpleShooter project.

Step 1:
Currently our line tracing collides with the capsule component of our character. So first we have to disable this.
Go in the Project Settings, Engine - Collision section, and expand presets. Open the Pawn preset. That’s the preset used by our character capsule component. Make it ignore the bullet channel.

Step 2:
Now we need to create a PhysicsAsset for our character skeletal mesh: “A Physics Asset is used to define the physics and collision used by a Skeletal Mesh.”
Basically we will have a mini capsule collision component for each bone of our skeletal mesh. Each of these are called a Physics Body.
Then we will add different Physics Materials to each Physics Body of our Physics Asset.

Just follow the first 6 minutes of this tutorial to do this:

Step 3:
Now we can get which material is hit with the line tracing in our Gun.cpp:

  • Add phys material in the query params of our line trace
FCollisionQueryParams QueryParams;
QueryParams.bReturnPhysicalMaterial = true;
LineTraceSingleByChannel(... , QueryParams);
  • Add “PhysicsCore” module in your .Build.cs

  • Add following import
    #include "PhysicalMaterials/PhysicalMaterial.h"

  • Then we can get the hit material in the FHitResult of the line trace:

UPhysicalMaterial* HitMaterial = HitResult.PhysMaterial.Get();
EPhysicalSurface HitSurface = UPhysicalMaterial::DetermineSurfaceType(HitMaterial); 
if (HitSurface == EPhysicalSurface::SurfaceType1) {
  UE_LOG(LogTemp, Warning, TEXT("Head shot"));
}

That’s it! Hope it can help some of you :slight_smile:

2 Likes

I stumbled through this as well on my mega challenge and found some scattered resources on the Reddit for a solution and wish I came across your post first. I used the physics asset in order to enable PointDamageEvent.HitInfo.BoneName to specify the bone name of the point damage event instead of “NONE” which is what happens without the physics asset. I encapsulated the bone matching behavior and applied a damage multiplier to the base damage such that a head shot would result in enough health damage to kill the enemy, etc.

Didn’t realize you can get the physics material as well. WIll try that out in my polishing phase.

Privacy & Terms