I want to use LineTraceSingleByChannel function for my SightRay component which will be used for my adventure game. I want to mark certain objects in game as 'Viewable' in Trace Channel. Then the ray will only be true if it hits a 'Viewable' object.

I have created the Viewable trace channel in the Collision settings of Project Settings. But I have two problems now.

  1. In the actual c++ code I am writing, how do I reference this new 'Viewable' channel in the ECollisionChannel parameter of the method?

  2. How do I assign this channel to each object in the scene?

I have been trying to figure this out for hours, I've read many Unreal documentation pages and forum threads but can't get a straight answer, particularly without using Blueprint nodes.

Here is my code:

bool USightRay::GetLineTraceHit(FHitResult &OutHit)

FVector LineStart = UGameplayStatics::GetPlayerCameraManager(this, 0)->GetCameraLocation();
FVector LineEnd = LineStart + UGameplayStatics::GetPlayerCameraManager(this, 0)->GetCameraRotation().Vector() * LineTraceLength;
//ECollisionChannel viewable =  WHAT CAN I PUT HERE?????
if (GetWorld()->LineTraceSingleByChannel(OutHit, LineStart, LineEnd, viewable))
    return true;

return false;

I actually thought this would be simple before I started to code it. I've used it before but I just used ECollisionChannel::ECC_Visibility in place of 'viewable'. This time I want to learn to filter the collisions and traces properly.

Thank you for any help.


Well I kind of figured it out, but thought I can post here for any comments in case I am doing anything wrong. Also this might help someone who reads this with same problem.

Basically, I had created the custom trace as mentioned, but I didn't really know how to access it in code. After hours of reading I found you must go to your projects DefaultEngine.ini file , in there search "ECC" and it will tell you which one it is.

Then you can put something like:

#define COLLISION_VIEWABLE ECC_GameTraceChannel2

at top of your cpp file, and in the method parameter use ECollisionChannel::COLLISION_VIEWABLE , this now raycasts only on my specified channel.

  • 1
    \$\begingroup\$ This is the right way! \$\endgroup\$
    – ColdSteel
    Feb 3 '20 at 10:32
  • \$\begingroup\$ Excellent thank you :) \$\endgroup\$
    – Krangogram
    Feb 3 '20 at 12:59
  • \$\begingroup\$ I saw this post when I was searching for reason why tracing by custom channel doesn't work. I thought that maybe the channel name defined by the macro must follow COLLISION_NAME pattern, it cannot be an arbitrary name. But I found that's not the point, you can define it as anything you want. There are EngineTraceChannel and GameTraceChannel in the ECC enum, I set the Engine one by mistake. Thank you 🤣 \$\endgroup\$ Jul 18 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.