Hi everyone, for some reason wen I create resources in my game some of them when set by the line trace, the are in the middle of the Air, here is a screenshot and the code of the Trace, any Idea of what could be making this happen would be great.
Screenshot:
and some Code:
void UMyClass::SpawnMethod(UWorld* const World)
{
if (World)
{
AWandererMyKingdomGameMode* GameMode = Cast<AWandererMyKingdomGameMode>(UGameplayStatics::GetGameMode(World));
CurrentPlayState = GameMode->GetPlayStateReference();
for (TActorIterator<APlayerStart> ActorItr(World); ActorItr; ++ActorItr)
{
APlayerStart* BuildingStartPoint = *ActorItr;
if (BuildingStartPoint->PlayerStartTag == BuildingSpawnPointName)
{
BuildingTempSpawnPoint = BuildingStartPoint;
break;
}
}
AStaticMeshActor* MapFloor = nullptr;
for (TActorIterator<AStaticMeshActor> ActorItr(World); ActorItr; ++ActorItr)
{
if (ActorItr->GetName() == MapName.ToString())
{
MapFloor = *ActorItr;
break;
}
}
if (MapFloor)
{
FVector Range = FVector::ZeroVector;
FVector FloorOrigin = FVector::ZeroVector;
FVector FloorBoxExtent = FVector::ZeroVector;
int MaxAmount = 0;
MapFloor->GetActorBounds(false, FloorOrigin, FloorBoxExtent);
MaxAmount = FMath::Abs(FloorBoxExtent.X * 0.1) / 3;
Range = FloorBoxExtent;
if (Range != FVector::ZeroVector)
{
// Here I start to create all the resources and add them to the map Randomly
FString ResourceName;
AResource* Resource;
EResourcesType ResourceType;
TArray<FVector> OldValues;
OldValues.Reserve(MaxAmount);
for (int i = 0; i < 3; ++i)
{
ResourceType = static_cast<EResourcesType>(i);
int Amount = 0;
switch (ResourceType)
{
case EResourcesType::Foods:
ResourceName = "Foods";
Amount = FMath::CeilToInt(MaxAmount * 0.30f);
break;
case EResourcesType::Woods:
ResourceName = "Woods";
Amount = FMath::CeilToInt(MaxAmount * 0.55f);
break;
case EResourcesType::Mines:
ResourceName = "Mines";
Amount = FMath::CeilToInt(MaxAmount * 0.15f);
break;
}
for (int j = 0; j < Amount; ++j)
{
bool rehit = true;
const FName TraceTag("MyTraceTag");
World->DebugDrawTraceTag = TraceTag;
FCollisionQueryParams collisionParams(FName(TEXT("FoliageClusterPlacementTrace")), true);
collisionParams.bReturnPhysicalMaterial = false;
collisionParams.TraceTag = TraceTag;
FHitResult Hit(ForceInit);
EResourcesSubType SubType;
float TraceX;
float TraceY;
while (rehit)
{
TraceX = FMath::FRandRange(Range.X, -1 * Range.X);
TraceY = FMath::FRandRange(Range.Y, -1 * Range.Y);
if (World->LineTraceSingleByObjectType(Hit,
FVector(TraceX, TraceY, 10000),
FVector(TraceX, TraceY, -10000),
FCollisionObjectQueryParams::AllObjects,
collisionParams))
{
if (MapFloor == Cast<AStaticMeshActor>(Hit.GetActor()))
{
FVector Location = FVector(Hit.Location.X, Hit.Location.Y, Hit.Location.Z);
if (ResourceType == EResourcesType::Mines && Amount <= 1)
{
SubType = EResourcesSubType::Stone;
}
else
{
SubType = EResourcesSubType::None;
}
rehit = false;
if (VerifyLocationDistance(OldValues, Location, FloorOrigin, FloorBoxExtent) && !rehit)
{
OldValues.Add(Location);
Resource = World->SpawnActor<AResource>(Location, FRotator(0, FMath::FRandRange(0.0f, 360.0f), 0));
Resource->SetResourceSite(ResourceType, SubType);
#if WITH_EDITOR
Resource->SetFolderPath(*("/Resources/" + ResourceName));
#endif
}
else
{
rehit = true;
}
}
}
}
}
}
}
}
}
}