As I’m having the same issue, here’s what I’m doing.
Note that I’m extending AActor.
My header file has the following:
UFUNCTION()
void OnBeginOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
And my cpp file has this:
In the constructor:
SpriteComponent->OnComponentBeginOverlap.AddDynamic(this, &AMob::OnBeginOverlap);
And then the function is just this. It’s being called but the hit information is empty:
void AMob::OnBeginOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
if (OtherActor != this) {
UE_LOG(LogTemp, Warning, TEXT("OnBeginOverlap"));
FVector Location = GetActorLocation();
if (!SweepResult.bStartPenetrating) {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Distance: %f"), SweepResult.Distance));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Pene: %f"), SweepResult.PenetrationDepth));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Loc: %f %f %f"), SweepResult.Location.X, SweepResult.Location.Y, SweepResult.Location.Z));
Location.Z += SweepResult.Distance;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Imp: %f %f %f"), SweepResult.ImpactNormal.X, SweepResult.ImpactNormal.Y, SweepResult.ImpactNormal.Z));
Location.Z += SweepResult.Distance;
} else {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Pene: %f"), SweepResult.PenetrationDepth));
Location.Z += SweepResult.PenetrationDepth;
}
SetActorLocation(Location);
}
}
Please note that I’m using Paper2D and that my collider is drawn on the SpriteComponent. I don’t know if that might be a problem.
The full test code can be found there if you want to have a look at it: GitHub - tanis2000/Unreal-Test2DPlatformer: A test project for a 2D platformer built with Unreal Engine 4