Download

FHitResult weird issue!

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.

You have to check if your trace is actually hitting anything.

if (LineTrace(…)) {
// then spawn…
}

Otherwise HitOut.ImpactPoint is just a default vector(0,0,0).

An hour after post this topic, I tried to recompile with the lines uncommented and it worked!

Sorry, my english is not so good, I uploaded two images, but the post doesn’t show them.
I started spawning some actors on a random x,y location and a fixed z coordination, because those actrors was on the floor.
Adding a landscape, which is non-flat(in my case), I try to find intersection between a ray and the landscape, the hit point is the z value for the actor.
So I added only a LineTraceSingleByChannel function which need a FHitResult and a FCollisionQueryParams structs.
Edited the code and compiled with no errors, when played, no actors were spawned, so I tried to comment all the new lines, restoring the code as before, recompiled and of course it spawned the actors again.
Then I started uncomment one line at a time, recompiling every time, and when I uncommented the “FHitResult HitOut…” line it doesn’t spawn anything.
The strange issue is that a simple variable declaration made the program unable to spawn actors!
Funny thing is that now it works and I touched nothing, not even rebooted the pc.
This strange thing happened even with the actors spawned normally on a flat surface, I tried to repeatedly play and stop the project a lot of times, and sometimes it doesn’t spawn actors.

Ok this is very weird, today I tried and it doesn’t work again, I don’t know if is some setting on the editor, maybe my pc?

Try to do this:



FCollisionQueryParams TraceParams(FName(TEXT("Trace")), true,this);
TraceParams.***bIgnoreTouches ***= true;

FHitResult HitOut = FHitResult(ForceInit);


Today I tried again to play and stop repeatedly the project and it worked everytime, with or without this new line.
Since I got this problem even in the first project with the spawns on a flat surface without a trace, if is not an editor setting, I thought that could be the way the numbers get randomized.
Is it possible?