Reason to retrun the HitResult an not just the HitActor

Is there a specific reason, why returning the HitResult in the new function an not the Actor which is Hit?

No real reason, feel free to just return the hit actor.

1 Like

Thanks @DanM !

I had a bit of struggel doing it by byself and this is what I came up with after some time, just if someone wanted to do the same and had some struggle doing it (working code):

//returns a Actor for a Trace with Params for PhysicBodys only
AActor* UGrabber::TracePhysicsBody() const
{
	FVector LineTraceEnd = GetGrabTraceEnd();
	FVector ViewpointLocation = GetViewpointLocation();

	FHitResult TraceHitResult;
	FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());

	//RayCast to X-Distance (reach)
	GetWorld()->LineTraceSingleByObjectType(
		OUT TraceHitResult,
		ViewpointLocation, 
		LineTraceEnd,
		FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), 
		TraceParams
	);

	AActor* HitActor = TraceHitResult.GetActor();;

	if(HitActor != nullptr) //DEBUG
	{
		UE_LOG(LogTemp, Warning, TEXT("LineTrace Hit %s"), *HitActor->GetName());
	}

	return HitActor;
}

void UGrabber::Grab()
{
	FVector LineTraceEnd = GetGrabTraceEnd();
	AActor* HitActor = TracePhysicsBody();
	//If Hit sth, then attach Actor to physics handle
	if(HitActor)
	{
		if(!PhysicsHandle) return; //Exit if no physicshandle
		UPrimitiveComponent* GrabComponent = HitActor->FindComponentByClass<UPrimitiveComponent>();
		PhysicsHandle->GrabComponentAtLocation
		(
			GrabComponent,
			NAME_None,
			LineTraceEnd
		);
	}

}


Cheers

Perhaps it would be better if you return the component that was hit instead?

1 Like

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

Privacy & Terms