C++ Line Trace Debug Color

I have successfully used both Trace Tags and DrawDebugLine to render debug lines, but I have an issue.

Is there a function for a C++ debug line that acts like the blueprint version (color features)? Trace Tags seem to only be white. I can’t find a way to adjust color for these. I can draw a debug line with a different color using DrawDebugLine, but it does not change color when a hit is detected. I can’t find any parameters that suggest this is possible.

In blueprint, LineTraceByChannel has “Trace Color” AND “Trace Hit Color”.

269152-2019-02-20-13-09-47-platformercpp-unreal-editor.png

I need “Trace Hit Color” in C++.

Thank You,

Ryan

When constructing the DrawDebug line , you can add a FColor and select the RGB Value

You are looking for: DrawDebugLine | Unreal Engine Documentation

Example:
if(GetWorld())
DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor(255, 0, 0), false, 5, 1, 3.f);

Could you please read the topic and question carefully before replying ? He is asking for Trace hit color not debug line color.
you can add another debug at the hit location with different color.

Of course, I will answer 3 years later, but in a better way.

I’m pretty sure he’s asking if there is a function like (BP) LineTraceByChannel, but for C++. Seems like he knows how to use it. So, here is how you use LineTraceByChannel in C++:

void UInteractionComponent::TestTrace()
{
  FHitResult HitResult;
  const FVector TraceStart = CharacterRef->GetActorLocation();
  const FVector TraceEnd = CharacterRef->GetActorLocation() - FVector(0, 0, 200);

  if (GetWorld()->LineTraceSingleByChannel(
      HitResult,
      TraceStart,
      TraceEnd,
      ECC_Visibility
  ))
  {
      // Do something
  }
}

However, you also want to see the colors. So you can use this, that is really the same than blueprint LineTraceByChannel node:

void UInteractionComponent::TestTrace()
{
    FHitResult HitResult;
    const FVector TraceStart = CharacterRef->GetActorLocation();
    const FVector TraceEnd = CharacterRef->GetActorLocation() - FVector(0, 0, 200);
    TArray<AActor*> ActorsToIgnore;
    FColor ColorBeforeHit = FColor::Red;
    FColor ColorAfterHit = FColor::Red;
    float ColorsLifeTime = 5.f;
    
    if (UKismetSystemLibrary::LineTraceSingle(
        GetWorld(),
        TraceStart,
        TraceEnd,
        UEngineTypes::ConvertToTraceType(ECC_Visibility),
        false,
        ActorsToIgnore,
        EDrawDebugTrace::ForDuration,
        HitResult,
        true,
        ColorBeforeHit,
        ColorAfterHit,
        ColorsLifeTime
    ))
    {
        // Do something
    }
}

By the way, note that you can can hover any node and check what library is this comming from. This way you can google it and find the function.

I recently found

#include "KismetTraceUtils.h"

void DrawDebugLineTraceSingle(
const UWorld* World, 
const FVector& Start, 
const FVector& End, 
EDrawDebugTrace::Type DrawDebugType, 
bool bHit, 
const FHitResult& OutHit, 
FLinearColor TraceColor, 
FLinearColor TraceHitColor, 
float DrawTime);

Which is probably what OP was looking for to replicate the same functionality in code that can be done through BP.

I’m using 5.4, but I’d guess its available in earlier versions of the engine as well.

From my understanding, an example usage of it works like below:

FHitResult Hit(1.f);
float TraceDistance = 500.f; float TraceDuration = 5.f;
FVector Start = GetActorLocation();
FVector End = Start + GetActorForwardVector() * TraceDistance;
FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(this);
UWorld* World = GetWorld();

// do the line trace
bool bHit = World->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, QueryParams);

// now draw a debug line using the result of the trace
DrawDebugLineTraceSingle(World, Start, End, EDrawDebugTrace::ForDuration, bHit, Hit, FColor::Red, FColor::Green, TraceDuration);

In addition, Since UE 5.4 the Chaos Visual debugger is also available (the 5.5 version has more features, but in 5.4 the basic functionality is available).

We record all scene queries done via the UWorld API, you can not only see the query itself visually, but you can also click on it and see all the parameter used with the query.

The visualization is not in the game viewport though.