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