I wrote a very ugly function to trace to the floor and get the surface type and play a footstep sound. The problem is that it crashes when I try assigning the surface type from the hit result to an EPhysicalSurface
variable. Here’s the whole function:
void AAdNoctvmPlayerCharacter::DoFootstep()
{
AAdNoctvmPlayerController* PC = Cast<AAdNoctvmPlayerController>(Controller);
FCollisionQueryParams Params = FCollisionQueryParams(FName(TEXT("FootstepTrace")), true, this);
FVector Start = GetActorLocation();
FVector End = Start + FVector(0, 0, -100);
FHitResult HitResult;
bool bHit = GetWorld()->LineTraceSingle(HitResult, Start, End, ECollisionChannel::ECC_PhysicsBody, Params);
if (PC)
{
if (bHit)
{
EPhysicalSurface Surf = HitResult.PhysMaterial->SurfaceType;
if (Surf == EPhysicalSurface::SurfaceType_Default)
{
PC->ClientPlaySoundAtLocation(DefaultFootstep, GetActorLocation(), 1, 1);
NoiseEmitter->MakeNoise(this, 1, GetActorLocation());
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, TEXT("Played footstep sound"));
}
else if (Surf == EPhysicalSurface::SurfaceType1) // brick
{
PC->ClientPlaySoundAtLocation(DefaultFootstep, GetActorLocation(), 1, 1);
NoiseEmitter->MakeNoise(this, 1, GetActorLocation());
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, TEXT("Played footstep sound"));
}
else if (Surf == EPhysicalSurface::SurfaceType2) // cobblestone
{
PC->ClientPlaySoundAtLocation(DefaultFootstep, GetActorLocation(), 1, 1);
NoiseEmitter->MakeNoise(this, 1, GetActorLocation());
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, TEXT("Played footstep sound"));
}
else if (Surf == EPhysicalSurface::SurfaceType3) // glass
{
PC->ClientPlaySoundAtLocation(TileFootstep, GetActorLocation(), 1, 1);
NoiseEmitter->MakeNoise(this, 1, GetActorLocation());
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, TEXT("Played footstep sound"));
}
else if (Surf == EPhysicalSurface::SurfaceType4) // gravel
{
PC->ClientPlaySoundAtLocation(DefaultFootstep, GetActorLocation(), 1, 1);
NoiseEmitter->MakeNoise(this, 1, GetActorLocation());
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, TEXT("Played footstep sound"));
}
else if (Surf == EPhysicalSurface::SurfaceType5) // wood
{
PC->ClientPlaySoundAtLocation(WoodFootstep, GetActorLocation(), 1, 1);
NoiseEmitter->MakeNoise(this, 1, GetActorLocation());
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, TEXT("Played footstep sound"));
}
else
{
return; // don't make a sound
}
}
}
}
This is the callstack: !Name:VibgyorAccess violation - code c0000005 (first/second chance not avail - Pastebin.com
The last line executed is EPhysicalSurface Surf = HitResult.PhysMaterial->SurfaceType;
Anyone know what’s wrong with my code?