How to ray cast?

Hello, so i was just wondering how i can make a simple ray cast within UE4 say for like weapons or AI? also some examples would be great! i seen a few other posts but the answers weren’t very clear so i was hoping to get some clarity with this :slight_smile:

You have a lot of that in GWorld:

  1. LineTraceTest - Trace a ray against the world using a specific channel and return if a blocking hit is found.
  2. LineTraceSingle - Trace a ray against the world using a specific channel and return the first blocking hit.
  3. LineTraceMulti - Trace a ray against the world using a specific channel and return overlapping hits and then first blocking hit. Results are sorted, so a blocking hit (if found) will be the last element of the array. Only the single closest blocking result will be generated, no tests will be done after that.

EDIT:

For example in Unreal Tournament, trace channels are defined in UnrealTournament.h.

#define COLLISION_PROJECTILE ECC_GameTraceChannel1
#define COLLISION_TRACE_WEAPON ECC_GameTraceChannel2
#define COLLISION_PROJECTILE_SHOOTABLE ECC_GameTraceChannel3
#define COLLISION_TELEPORTING_OBJECT ECC_GameTraceChannel4
#define COLLISION_PAWNOVERLAP ECC_GameTraceChannel5
#define COLLISION_TRACE_WEAPONNOCHARACTER ECC_GameTraceChannel6

And then used from many places such as: UTGameplayStatics::ComponentIsVisibleFrom(…).

FCollisionQueryParams LineParams(FName(TEXT("ComponentIsVisibleFrom")), true, IgnoredActor);

bool const bHadBlockingHit = World->LineTraceSingle(OutHitResult, TraceStart, TraceEnd, COLLISION_TRACE_WEAPON, LineParams);

Where TraceStart and TraceEnd are vectors, OutHitResult is an out parameter that have result of your hit test.

hey thanks that was really helpful! so somthing like this would work?

 #define COLLISION_TRACE ECC_GameTraceChannel1

FHistResult Hit;//the thing that is an output of the statement

FCollisionQueryParams Line(FName("Collision param"),true);

World->LineTraceSingle(Hit, this->Location /*cant remember the actual name for the variable :/ */, this->RelativeLocation.X += 100,COLLISION_TRACE,Line);

AEnemy* enemy = Cast<AEnemy>(Hit.GetActor());

if(enemy){
enemy->Destroy();
}

Looks fine, remember to set channels properly:

thank you very much for your help :slight_smile: really appreciate it

If you consider this answer as helpful, please close question so that thers will know that issue is solved.