I’m trying to get all Actors that are overlapping a box. For this purpose I created this function:
template <typename T>
static void GetActorsInArea(const UObject* WorldContextObject, TArray<T*>& OutActors, const FVector& BoxCenterLocation, float BoxExtent = 0)
{
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
{
UE_LOG(LogTemp, Warning, TEXT("START"))
TArray<FHitResult> HitResults;
FVector StartLocation = BoxCenterLocation + FVector(0, 0, 100);
FVector EndLocation = BoxCenterLocation - FVector(0, 0, 100);
FCollisionShape CollisionShape;
CollisionShape.SetBox(FVector(BoxExtent));
FCollisionQueryParams QueryParams;
QueryParams.bIgnoreBlocks = true;
World->SweepMultiByChannel(HitResults, StartLocation, EndLocation, FQuat(), ECC_WorldDynamic, CollisionShape, QueryParams);
//World->SweepMultiByObjectType(HitResults, StartLocation, EndLocation, FQuat(), FCollisionObjectQueryParams(0), CollisionShape, QueryParams);
for (FHitResult& HitResult : HitResults)
{
if (T* ValidActor = Cast<T>(HitResult.GetActor()))
{
UE_LOG(LogTemp,Warning,TEXT("ADD"))
OutActors.Add(ValidActor);
DrawDebugBox(World, BoxCenterLocation, FVector(BoxExtent + 10), FColor::Blue, false, 100);
}
}
DrawDebugBox(World, BoxCenterLocation, FVector(BoxExtent + 5), FColor::Red, false, 100);
UE_LOG(LogTemp, Warning, TEXT("END"))
}
}
The function is called for each location that is represented by a green debug sphere. The problem is that the tiles are detected in every second function call (in other cases nothing is detected). I do not know where I make the mistake, so I am asking you for help. (The cubes and floor have disabled collisions)