LineTraceSingleByChannel C++

Tell me how to write it in C++?

FVector Start = FVector(10,10,10);
FVector End = FVector(20, 20, 20);
FHitResult HitResult;
FCollisionQueryParams  COQP;
COQP.AddIgnoredActor(this);						
FCollisionResponseParams CollRes;

GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECollisionChannel::ECC_Visibility, COQP, CollRes);
1 Like

Thanks, but I’m a complete newbie. I didn’t understand how
check the condition and write the value to variables 2, 3, 4, 6 and 7?

			FVector Start = FVector(10,10,10);
			FVector End = FVector(20, 20, 20);
			FHitResult HitResult;
			FCollisionQueryParams  COQP;
			COQP.AddIgnoredActor(this);						
			FCollisionResponseParams CollRes;
			
			AActor* NewVar2 = nullptr;
			UPrimitiveComponent* NewVar3 = nullptr;
			FName NewVar4;			
			float NewVar6;
			FVector NewVar7;

			
			if (GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECollisionChannel::ECC_Visibility, COQP, CollRes)) {				
				if (HitResult.bBlockingHit) {
					NewVar2 = HitResult.GetActor();
					NewVar3 = HitResult.GetComponent();
					NewVar4 = HitResult.BoneName;
					NewVar6 = HitResult.Distance;
					NewVar7 = HitResult.Location;
				}
			}

1 Like

Also if you want the full line-trace based on the kismet library (as seen in blueprints) you can use this:

			float DrawTime = 5.0f;						
			ETraceTypeQuery TraceChannel = UEngineTypes::ConvertToTraceType(ECollisionChannel::ECC_Visibility);
			const TArray<AActor*> ActorsToIgnore;			
			if (UKismetSystemLibrary::LineTraceSingle(GetWorld(), Start, End, TraceChannel, false, ActorsToIgnore, EDrawDebugTrace::None, HitResult, true, FColor::Red, FColor::Green, DrawTime)) {
				if (HitResult.bBlockingHit) {
					NewVar2 = HitResult.GetActor();
					NewVar3 = HitResult.GetComponent();
					NewVar4 = HitResult.BoneName;
					NewVar6 = HitResult.Distance;
					NewVar7 = HitResult.Location;
				}
			}

You will need to include the following header then

#include "Kismet/KismetSystemLibrary.h" 
#include "Engine/CollisionProfile.h" 
2 Likes

I pasted your code and it worked. This is exactly what I need! Thanks you’re a real friend.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.