LineTraceSingleByChannel always returns true

Hi all. I’m attempting to determine whether a box is sitting in front of my character with a raycast. I’m currently doing so by calling LineTraceSingleByChannel in Tick. I have created a custom channel called “Pickupable” in the project settings, and set the box Object Type to this channel. However, when I run the game, LineTraceSingleByChannel returns true no matter how far the character is from the box, and no matter its orientation. I’ve added a debug line and the raycast appears where I’m expecting it. Why is the function behaving this way? Is the raycast colliding with the character itself, even though it’s on a different channel? I’m a newbie so there must be something fundamental I’m misunderstanding about the function. Any help would be appreciated.

Here is the tick function:


void APracticeCharacter::Tick(float DeltaTime)
{
FHitResult hitResult;
FVector start = GetActorLocation() - FVector(0.0f, 0.0f, 30.0f);
FVector end = start + 100.0 * GetActorForwardVector();
DrawDebugLine(GetWorld(), start, end, FColor(255, 0, 0), false, -1, 0, 5.0);

bool isHit = GetWorld()->LineTraceSingleByChannel(hitResult, start, end, ECollisionChannel::ECC_GameTraceChannel1);

if (isHit)
{
UE_LOG(LogTemp, Warning, TEXT("I hit a thing! "), *hitResult.Actor->GetName());
}
}

Quick update: From debugging it looks like the ray is hitting the character itself, which is weird since I didn’t set the character to the pickupable channel… I’ve shared the character’s collision settings in the picture.

I’ve replaced the function with LineTraceSingleByObjectType, and it works as I expect it to. Can someone explain what the channel means then?

1 Like

you must ignore your self while raycasting.


FCollisionQueryParams Params;

Params.AddIgnoredActor(this);


bool isHit = GetWorld()->LineTraceSingleByChannel(hitResult, start, end, ECollisionChannel::ECC_GameTraceChannel1, Params);

1 Like

Oh, well that’s a reasonable explanation. Thanks!

Thank you for testing that LineTraceSingleByObjectType() works.
I was wondering why this doesnt work but it occured to me that LineTraceSingleByChannel() will get hits on every channels that have in collision settings set BLOCK to trace channel from argument (f.e ECollisionChannel::ECC_GameTraceChannel1).

You can see the settings for each channels in Project Settings->Collision->Preset

If we only want hits on specific channel then we have to use LineTraceSingleByObjectType().