[C++] How to ignore the Character Actor while line tracing?

Hi there, sorry to bother you with this guys, but

Following Orfeas tutorial on simple line raycasting, I’m trying to mess around with the returned actor of the FHitResult struct.
I started with the First Person C++ template and wrote the basic parameters and function to use linetrace inside the fire method from the character class ( FHitResult Hit and float RayLength are declared within the header file).

void FirstPersonCharacter::OnFire() {
[...]
/* RAYCAST */
// Vectors
FVector StartLocation = FirstPersonCameraComponent->GetComponentLocation(); // Origin
FVector EndLocation = StartLocation + (FirstPersonCameraComponent->GetForwardVector() * RayLength); // End

// Params
FCollisionQueryParams CollisionParameters;
CollisionParameters.bFindInitialOverlaps = false; // Doesn't change anything whether it's true by default or false in my case
CollisionParameters.AddIgnoredActor(this);
//CollisionParameters.AddIgnoredActor(GetCapsuleComponent()->GetOwner()); // I also trid this
//CollisionParameters.AddIgnoredComponents(GetCapsuleComponent()->GetOwner()->GetComponents()); // And this

// Line trace
ActorLineTraceSingle(Hit, StartLocation, EndLocation, ECollisionChannel::ECC_WorldDynamic, CollisionParameters);
// Debug Line Draw
DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor::White, false, 10, 0, 1.f);

GLog->Log(Hit.ToString());
// Getting the hit actor
AActor* HitActor= Hit.GetActor();
}

By logging the FHitResult it tells me it hit the CollisionCylinder from attached to the character:

bBlockingHit:False bStartPenetrating:True Time:0.0 [...] Actor:FirstPersonCharacter_C_1 Component:CollisionCylinder

So how should I ignore the Character AActor ? Do I need to ignore both the Actor and his Components ?
I tried unsuccessfully both (separately) as you can tell from code.
And how should I get the instance of the character class ?
(Tried this within the class and GetComponent()->GetOwner() )

Thank you for your time. =)

For your problem, i can suggest 2 action:

  1. Check out this tutorial A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums, instead of using ActorLineTraceSingle, this tutorial use World->LineTraceSingle, with pretty much everything else the same as your tutorial, all in order to see if you will get the same result.
  2. You can use LineTraceMulti, this function will return a list of all the hit objects, and then all you have to do is remove your ignored characters from the list and get the first hit result.

When you are doing a line trace single, you can also set a Actor to ignore for the Trace Params, when it’s constructed, there are also functions for the trace params object, to add actors, and components to ignore as well.

.

1 Like

Thank you, it worked like a charm.
I don’t understand the fundamental differences of the technical implementations but it does the trick. I’ll dig some more on my side.
Thanks again. =D

(Anyway I can close my question by setting it to some kind of “solved” state ?)

FCollisionQueryParams CollisionParameters;
CollisionParameters.AddIgnoredActor(this);
Tried it.
As CKong mentionned, it only works when you’re firing a trace from the world ( code>GetWorld()->LineTraceSingle() ) and not the actor ( ActorLineTraceSingle() ).

2 Likes