LineTrace working different in c++ then blueprint

Hello,

I setted up the linetraces following a tutorial and it works pretty well

When setting this up in c++ I’m not seeing those “hit blocks”, the lines just go through everything.

Here is the code

FHitResult HitResult(ForceInit);
	// Calculate the direction we are 'looking'  
	FVector CamLoc;
	FRotator CamRot;
	GetActorEyesViewPoint(CamLoc, CamRot);
	// Calculate the start location for trace  
	FVector StartTrace;
	FRotator UnusedRotation;
	GetActorEyesViewPoint(StartTrace, UnusedRotation);
	// Adjust trace so there is nothing blocking the ray between the camera and the pawn, and calculate distance from adjusted start  
	StartTrace = StartTrace + CamRot.Vector();

	// Distance to trace  
	const float WeaponRange = 5000.0f;
	// Calculate endpoint of trace  
	const FVector EndTrace = StartTrace + FirstPersonCameraComponent->GetForwardVector() * WeaponRange;

	// Setup the trace query  
	static FName FireTraceIdent = FName(TEXT("WeaponTrace"));
	FCollisionQueryParams TraceParams(FireTraceIdent, true, this);
	TraceParams.bTraceComplex = true;
	TraceParams.bTraceAsyncScene = false;
	TraceParams.bReturnPhysicalMaterial = false;

	// Perform the trace  
	GetWorld()->LineTraceSingle(HitResult, StartTrace, EndTrace, ECC_Pawn, TraceParams);
	
	DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor::, false, 10.f);

Can someone please tell me what is different ?

Thanks a lot

Hi ,

Can you explain what exactly is missing from the result of your line trace that you’re doing through code? Is the debug line not being drawn correctly? Also, what is being done in the blueprint from the “Out Hit” and “Return Value” pins?

Hi ,

sorry I should attach the screesnhot directly.
The left line trace (the square) is when I’m using linetrace from Blueprint, while the right line is from c++.
And I wonder what is the issue with my c++ linetrace that it does not show a square when hitting an acotr

Can you also post a screenshot of what is being done as a result of this Line Trace in your blueprint? Your original screenshot shows how the line trace is being done, but doesn’t show what it does after it hits a target as that part is cut off. This is probably the part that is drawing the square you want, but I would need to see this to let you know what is missing from your code version.

Hi,

please find the bp picture without the part cut off

GetActorEyesViewPoint(StartTrace, UnusedRotation);

Are you sure that this returns same vector as First Person Camera Component → Get World Location nodes ? You can try to log that value.

Also:

Code: TraceParams.bTraceComplex = true; | Blueprint: Trace Complex = false

Code: ECC_Pawn | Blueprint: Visibility

Hi ,

To get the square at the hit location, you should be able to call DrawDebugPoint() at Hit.ImpactPoint to draw it. The only difference other than that in your code compared to the blueprint’s code is that the blueprint uses GetWorldLocation of the actor instead of GetActorEyesViewPoint.

Hope this helps!