I have done this before successfully and for some reason the decal will not spawn anymore. I have the correct collision channels ticked I also created my own specific collision channel for the linetrace that is going to create the exit hole. One day I don’t know what happened now either my code is now irrelevant or the engine has a bug. I don’t understand why this is happening.
Here is the code:
FCollisionResponseParams ResponseParams;
FCollisionQueryParams RifleQueryParams = FCollisionQueryParams::DefaultQueryParam;
FActorSpawnParameters SpawnParams;
RifleQueryParams.bReturnPhysicalMaterial = true;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
RifleQueryParams.AddIgnoredComponent(WeaponBaseMesh);
TArray<FHitResult>Hit;
const FVector RifleStartTrace = WeaponBaseMesh->GetSocketLocation(FName("BarrelSocket"));
FRotator CurrentRotation = WeaponBaseMesh->GetSocketRotation(FName("BarrelSocket"));
FVector EndTrace = RifleStartTrace + CurrentRotation.Vector() * 10000.0f;
DrawDebugLine(GetWorld(), RifleStartTrace, EndTrace, FColor::Red, false, 1.0f, 0, 1.0);
//Here is the multiline trace by channel causing the first hit.
if (GetWorld()->LineTraceMultiByChannel(Hit, RifleStartTrace, EndTrace, ECollisionChannel::ECC_GameTraceChannel1, RifleQueryParams, ResponseParams))
{
for (FHitResult& Results : EnemyHit)
{
//Here is the first result.
if (AEnemy* Enemy = Cast<AEnemy>(Results.GetActor()))
{
FHitResult LineTraceEndHit2;
FCollisionResponseParams ResponseLineParams;
FCollisionQueryParams LineQueryParams = FCollisionQueryParams(SCENE_QUERY_STAT(WeaponTrace), false, this);
FActorSpawnParameters LineSpawnParams;
LineSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
LineQueryParams.AddIgnoredComponent(WeaponBaseMesh);
const FVector ExitLineStartTrace = StartTrace;
FRotator ExitLineCurrentRotation = EndTrace.Rotation();
FVector ExitLineEndTrace = EndTrace + ExitLineCurrentRotation .Vector();
DrawDebugLine(GetWorld(), ExitLineStartTrace , ExitLineEndTrace , FColor(255, 0, 0), false, -1, 0, 5.333);
//Here is the second line trace. I don't understand why the second is not firing off correctly I know I am attaching it to the exit point of the first line trace which hits the character. Now I know the second line trace much activate in order to create a exit hole.
if (GetWorld()->LineTraceSingleByChannel(LineTraceEndHit2, ExitLineStartTrace , ExitLineEndTrace, ECollisionChannel::ECC_GameTraceChannel4, LineQueryParams , ResponseLineParams))
{
//Here is where the second line trace fires once the enemy has been hit.
if (AActor* Actor = Cast<AActor>(LineTraceEndHit2.GetActor()))
{
GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Orange, FString::Printf(TEXT("Blood Has spawned")));
FVector DecalSize(20.0f, 20.0f, 20.0f);
FRotator RandomDecalRotation = LineTraceEndHit2.TraceEnd.Rotation();
RandomDecalRotation = FRotator(90,0,0);
FVector DecalLocation = LineTraceEndHit2.Location;
UGameplayStatics::SpawnDecalAtLocation(Actor, BloodDecal, DecalSize, DecalLocation, RandomDecalRotation, 10.0f);
}
}