Is it possible to have a BoxCollision detect LineTraceSingleForObjects?

Rather than having the LineTraceSingleForObjects detect the BoxCollision, I want the BoxCollision to start the OnComponentBeginOverlap event when it detects the line traces through it. Is this even possible? Thanks in advance.

A line trace does not inform the component it hits about the hit.

You could in theory call the OnComponentBeginOverlap.Broadcast yourself with data from your trace but I wouldn’t recommend that.

Instead I would create a second delegate the listener can listen two and call a function triggering that,
if you want to trigger this from overlaps and the race you can call this from the overlap as well.

MyBoxComponent.h

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnTrigger, AActor*, Source);

UCLASS(meta=(BlueprintSpawnableComponent))
class UMyBoxComponent: public UBoxComponent
{
	GENERATED_BODY()

public:
	virtual void BeginPlay() override;
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
	
	UFUNCTION()
	void ForwardTriggerFromOvlerap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
		int OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	UFUNCTION(BlueprintCallable)
	void TriggerTraceHit(AActor* Source);
	
	UPROPERTY(BlueprintAssignable)
	FOnTrigger OnTrigger;
};

MyBoxComponent.cpp

void UMyBoxComponent::BeginPlay()
{
	Super::BeginPlay();

	OnComponentBeginOverlap.AddDynamic(this, &ThisClass::ForwardTriggerFromOvlerap);
}

void UMyBoxComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	OnComponentBeginOverlap.RemoveDynamic(this, &ThisClass::ForwardTriggerFromOvlerap);
	
	Super::EndPlay(EndPlayReason);
}

void UMyBoxComponent::ForwardTriggerFromOvlerap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	TriggerTraceHit(OtherActor);
}

void UMyBoxComponent::TriggerTraceHit(AActor* Source)
{
	OnTrigger.Broadcast(Source);
}

Alternatively you could also invert the problem and test only one box collider against a trace and do whatever you want to do if you hit.

	/** Perform a line trace against a single component
	 * @param TraceStart The start of the trace in world-space
	 * @param TraceEnd The end of the trace in world-space
	 * @param bTraceComplex Whether or not to trace the complex physics representation or just the simple representation
	 * @param bShowTrace Whether or not to draw the trace in the world (for debugging)
	 * @param bPersistentShowTrace Whether or not to make the debugging draw stay in the world permanently
	 */
	UFUNCTION(BlueprintCallable, Category="Collision", meta=(DisplayName = "Line Trace Component", ScriptName = "LineTraceComponent", bTraceComplex="true", bPersistentShowTrace="false", UnsafeDuringActorConstruction="true"))	
	ENGINE_API bool K2_LineTraceComponent(FVector TraceStart, FVector TraceEnd, bool bTraceComplex, bool bShowTrace, bool bPersistentShowTrace, FVector& HitLocation, FVector& HitNormal, FName& BoneName, FHitResult& OutHit);

calling K2_LineTraceComponent should do the trick.

2 Likes

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