How would i use line trace in c++

Here’s the whole code you need to do a line trace in the c++

Enjoy!

FCollisionQueryParams RV_TraceParams = FCollisionQueryParams(FName(TEXT("RV_Trace")), true, this);
RV_TraceParams.bTraceComplex = true;
RV_TraceParams.bTraceAsyncScene = true;
RV_TraceParams.bReturnPhysicalMaterial = false;

//Re-initialize hit info
FHitResult RV_Hit(ForceInit);
	
//call GetWorld() from within an actor extending class
GetWorld()->LineTraceSingle(
	RV_Hit,		//result
	Start,	//start
	End, //end
	ECC_Pawn, //collision channel
	RV_TraceParams
);

once you do the trace,

RV_Hit.bBlockingHit //did hit something? (bool)
RV_Hit.GetActor(); //the hit actor if there is one
RV_Hit.ImpactPoint;  //FVector
RV_Hit.ImpactNormal;  //FVector
3 Likes

ahh i get it now so can i start a method with the RV_Hit as a param and check whats hitting my actor?

right i tried doing
if(RV_Hit.GetActor()->GetActorClass() == AMyProjectCharacter()){
}
but that won’t work because i need the params how do i do an if statement to do something if i hit a certain actor.

something like this should work!

if(RV_Hit.bBlockingHit){
  if(RV_Hit.GetActor()->IsA(AMyProjectCharacter::StaticClass()))
  {
     AMyProjectCharacter* HitCharacter = Cast(RV_Hit.GetActor());
   if(!HitCharacter) return;

   //do stuff to the HitCharacter
  }
}

if this answers your question please mark the check next to my answers so that others know your issues is resolved :slight_smile:

FHitResult Hit = GetWorld()->LineTraceSingle(parameters here). LineTraceSingle will return a hit result called Hit which you will have to define and with hit you can manipulate it like Hit.Location. For more reference look inside the header file in the Rocket directory called World.h. This should help you and now for your information there are different kinds of traces like trace rectangle or sphere with an area that you can trace, like in Unity. I hope this helps!

but linetraceSingle returns a boolean

Hi. are you active?
ECC_Pawn, //collision channel
I want my vehicle to hit the floor, what do i change this to?

Hey Moynzy-

This is an archived post and there have been many changes to the engine since this was last seen. If you are seeing a similar issue in a more current engine, please create a new post to track the issue you’re having.

Cheers

For users with a newer engine version

Replace LineTraceSingle with LineTraceSingleByChannel

i’m having trouble with the hit.getactor it’s telling me that hit is undefined

AAI_Character* AI = Cast<AAI_Character>(Hit.GetActor());

	FHitResult Hit(ForceInit);
	FVector start = FP_Gun->GetForwardVector();
	FVector End = start + (FP_Gun->GetForwardVector()* 700.f);
	FCollisionQueryParams CollisionParams;
	FVector Start = FP_Gun->GetComponentLocation();

	DrawDebugLine(GetWorld(), Start, End, FColor::Green, true, 2.f, false, 4.f);

	GetWorld()->LineTraceSingleByChannel(Hit, start, End, ECC_WorldDynamic, CollisionParams);

	if (Hit.GetActor())
	{
		

		AI->damage(1.f);

	}

watch this. How To Do Line Tracing Using C++ In Unreal Engine - YouTube