Hi guys, I’m relatively new with UE4, I’m still learning. I know c++ and I’m deepening the templates. I followed the various tutorials for learning the basics and I decided to make a simple project.
So I made a class that acts like a volume in which some actors get spawned at random x,y position, no problem with that, so I decided to do the same thing in a non-flat surface like a landscape.
For finding the right height value, I thought to do a line trace with LineTraceSingleByChannel, the problem is I can’t test it because I get no compile errors but it happen a strange thing when I play the project.
The version is 4.18, I have 2 simple classes: ASpawnZone and ASpawnObject and a simple landscape.
https://forums.unrealengine.com/core/image/gif;base64
The brown box is the spawn volume.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpawnZone.generated.h"
UCLASS()
class DEDEGAME2_API ASpawnZone : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpawnZone();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditDefaultsOnly, Category = SpawnClass)
TSubclassOf<class ASpawnObject> SpawnClass;
UPROPERTY(EditAnywhere, Category = Objects)
int m_nObjectsNum;
};
void ASpawnZone::BeginPlay()
{
Super::BeginPlay();
class UWorld *TheWorld = GetWorld();
if (TheWorld) {
FActorSpawnParameters ActorSpawnParams;
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
FVector vVolumeOrigin, vVolumeBounds, vRandomPos;
GetActorBounds(false, vVolumeOrigin, vVolumeBounds);
FMath::RandInit(FDateTime::Now().GetMillisecond());
FCollisionQueryParams TraceParams(FName(TEXT("Trace")), true);
//FHitResult HitOut = FHitResult(ForceInit);
for (int o = 0; o < m_nObjectsNum; o++) {
vRandomPos.X = vVolumeOrigin.X + FMath::RandRange(-vVolumeBounds.X, vVolumeBounds.X);
vRandomPos.Y = vVolumeOrigin.Y + FMath::RandRange(-vVolumeBounds.Y, vVolumeBounds.Y);
vRandomPos.Z = vVolumeBounds.Z; //upper point
//TheWorld->LineTraceSingleByChannel (HitOut, vRandomPos, FVector(vRandomPos.X, vRandomPos.Y, -vVolumeBounds.Z), ECC_Pawn, TraceParams); //trace
TheWorld->SpawnActor<ASpawnObject>(SpawnClass, vRandomPos, FRotator(), ActorSpawnParams);
//TheWorld->SpawnActor<ASpawnObject>(SpawnClass, FVector(HitOut.ImpactPoint().X, HitOut.ImpactPoint().Y, HitOut.ImpactPoint().Z), FRotator(), ActorSpawnParams);
}
}
}
I set “m_nObjectsNum” in the editor and if I compile and play with those commented lines, it works and spawns, let’s say, 100 actors at random positions, in the top of the spawn volume.
https://forums.unrealengine.com/core/image/gif;base64
but if I uncomment only this line
FHitResult HitOut = FHitResult(ForceInit);
it doesn’t give me errors but it doesn’t spawn anything.
Am I doing something wrong?
Don’t know what it is but maybe is happened something similar to you too.