Alternative to bringing container to the ground

I did not like the idea of moving the entire level we built just for the sake of having the container sit on the ground, I decided to add another translation after setting the randomly generated position:

void ATile::PlaceActors(TSubclassOf<AActor> ToSpawn, int MinSpawn, int MaxSpawn)
{
    FVector Min(0, -2000, 0);
    FVector Max(4000, 2000, 0);
    FBox Bounds(Min, Max);
    int NumberToSpawn = FMath::RandRange(MinSpawn, MaxSpawn);
    for (size_t i = 0; i < NumberToSpawn; i++)
    {
        FVector SpawnPoint = FMath::RandPointInBox(Bounds);
        UE_LOG(LogTemp, Warning, TEXT("SpawnPoint: %s"), *SpawnPoint.ToCompactString());
        AActor* Spawned = GetWorld()->SpawnActor<AActor>(ToSpawn);
        Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
        Spawned->SetActorRelativeLocation(SpawnPoint);
        SpawnedActors.Add(Spawned);
        Spawned->AddActorLocalOffset(FVector(0, 0, -120));
    }
}

That still has the same issue, really. You just moved it from the level to the code.

What you could do is do a line trace from the random point downwards and then spawn it at the hit location.

Yes basically, i just was not fond of the idea of moving the entire level just to accommodate for the prop objects so thought it was better to contain it within the C++ class.

Interesting thing you bring up though, from what you mentioned I tried looking up how to potentially do a line trace downwards and try getting the hit location, and I managed to get something like this:

code:

void ATile::PlaceActors(TSubclassOf<AActor> ToSpawn, int MinSpawn, int MaxSpawn)
{
    FVector Min(0, -2000, 0);
    FVector Max(4000, 2000, 0);
    FBox Bounds(Min, Max);
    int NumberToSpawn = FMath::RandRange(MinSpawn, MaxSpawn);
    for (size_t i = 0; i < NumberToSpawn; i++)
    {
        FVector SpawnPoint = FMath::RandPointInBox(Bounds);
        UE_LOG(LogTemp, Warning, TEXT("SpawnPoint: %s"), *SpawnPoint.ToCompactString());
        AActor* Spawned = GetWorld()->SpawnActor<AActor>(ToSpawn);
        Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
        //FVector StartTrace = Spawned->GetActorLocation();
        //GetWorld()->LineTraceSingleByChannel(*hitResult, SpawnPoint, EndTrace, ECC_Visibility);
        Spawned->SetActorRelativeLocation(SpawnPoint);
        FVector StartPoint = Spawned->GetActorLocation();
        FVector EndTrace = StartPoint - FVector(0, 0, 200);
        //FHitResult hitResult;
        FHitResult* hitResult2 = new FHitResult();
        DrawDebugLine(GetWorld(), StartPoint, EndTrace, FColor::Green, true, -1, 0, 10);
        //FCollisionQueryParams CollisionParameters;
        //TArray<AActor*> ignoredActors;
        //ignoredActors.Add(Spawned);
        //CollisionParameters.AddIgnoredActors(ignoredActors);
        //Spawned->ActorLineTraceSingle(hitResult, StartPoint, EndTrace, ECollisionChannel::ECC_WorldStatic, CollisionParameters);
        GetWorld()->LineTraceSingleByChannel(*hitResult2, StartPoint, EndTrace, ECC_Visibility);
        //UE_LOG(LogTemp, Warning, TEXT("HitPoint: %s"), *hitResult.ToString());
        UE_LOG(LogTemp, Warning, TEXT("HitPoint: %s"), *hitResult2->ToString());
        Spawned->SetActorLocation(hitResult2->Location);
        SpawnedActors.Add(Spawned);
        //Spawned->AddActorLocalOffset(FVector(0, 0, -120));
    }
}

I first tried using LineTraceSingleByChannel only for it to basically not work, and then I tried using Spawned->ActorLineTraceSingle and that didn’t work either, until I realized the problem was that i was grabbing the local/relative position, so after grabbing the world position and used the latter again, but I guess this one didn’t register a hit? I really am not sure why. Then I went back to LineTraceSingleByChannel and it seemed to work, though i have to now actually set the position of the prop actors in world space. It would be nice to convert it back to relative space and and apply the transform that way (though probably unnecessary).

It would be great if @DanM can provide some feedback on whether i approached this correctly…

This is unnecessarily allocating memory on the heap. There’s no need to do that.


Instead of spawning the actor and then line tracing and moving it. Just use the position you already generated from FMath::RandPointInBox

Also ActorLineTraceSingle traces the actor’s components

Trace a ray against the Components of this Actor and return the first blocking hit

Thank you for commenting on hitResult2, I didn’t realize that for LineTraceSingleByChannel() did not require the FHitResult object to be a pointer (since I have never used that before, but I did feel was strange since all other LineTrace methods I saw did not use a pointer).

To your second point, I actually initially tried using SpawnPoint because like you mentioned it made sense to me at the time, but it wasn’t really working and giving me the correct HitLocation so I started to draw DebugLines and and I saw that for some of the containers it drew the correct line but just gave me all of these other weird lines and I started to wonder why.

And then later I realized that these other “floating lines” were actually just the DebugLines for the other props but for the NEXT MAP, meaning because the coordinates were still in local space to the original Tile, therefore was not entirely correct on where they should be within the world.

You can observe that the DebugLines on the left corresponds to the container props stored on the right

So I figured this is why GetWorld()->LineTraceSingleByChannel() was failing to provide the correct HitLocation because it was not considering the coordinates was local to the Tile and grabbing the HitLocation based on it being world space. And I also noticed that when I try to utilize just the local space coords then at some point the placement for the props just kind of gets stuck at the origin of the BP_Tile component:

Then I noticed in the logs that at that point the HitResult actually returns false:

LogTemp: Repeating last play command: Selected Viewport
LogBlueprintUserMessages: Early PlayInEditor Detection: Level '/Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel' has LevelScriptBlueprint '/Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.FirstPersonExampleMap' with GeneratedClass '/Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap
_C' with ClassGeneratedBy '/Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.FirstPersonExampleMap'
LogPlayLevel: PlayLevel: No blueprints needed recompiling
PIE: New page: PIE session: FirstPersonExampleMap (Dec 30, 2020, 3:56:03 AM)
LogPlayLevel: Creating play world package: /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap
LogPlayLevel: PIE: StaticDuplicateObject took: (0.018853s)
LogAIModule: Creating AISystem for world FirstPersonExampleMap
LogPlayLevel: PIE: World Init took: (0.000781s)
LogPlayLevel: PIE: Created PIE world by copying editor world from /Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap to /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap (0.019888s)
LogInit: XAudio2 using 'Speakers (Realtek High Definition Audio)' : 2 channels at 48 kHz using 32 bits per sample (channel mask 0x3)
LogInit: FAudioDevice initialized.
LogLoad: Game class is 'InfiniteTerrainGameMode_C'
LogWorld: Bringing World /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap up for play (max tick rate 0) at 2020.12.30-11.56.03
LogWorld: Bringing up level for play took: 0.004008
LogTemp: Warning: SpawnPoint: V(X=3355.45, Y=-1169.29)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598167 Location:X=3355.449 Y=-1169.286 Z=-119.633 ImpactPoint:X=3355.449 Y=-1169.286 Z=-119.633 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=3355.449 Y=-1169.286 Z=0.000 TraceEnd:X=3355.449 Y=-1169.286 Z=-200.000 PenetrationDepth:0.000000 Item:-
1 PhysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=2930.51, Y=-1808.10)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598192 Location:X=2930.509 Y=-1808.100 Z=-119.638 ImpactPoint:X=2930.509 Y=-1808.099 Z=-119.638 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=2930.509 Y=-1808.100 Z=0.000 TraceEnd:X=2930.509 Y=-1808.100 Z=-200.000 PenetrationDepth:0.000000 Item:-
1 PhysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=3660.27, Y=-377.03)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598149 Location:X=3660.268 Y=-377.026 Z=-119.630 ImpactPoint:X=3660.268 Y=-377.026 Z=-119.630 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=3660.268 Y=-377.026 Z=0.000 TraceEnd:X=3660.268 Y=-377.026 Z=-200.000 PenetrationDepth:0.000000 Item:-1 Ph
ysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=1951.84, Y=1346.17)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598251 Location:X=1951.842 Y=1346.171 Z=-119.650 ImpactPoint:X=1951.842 Y=1346.171 Z=-119.650 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=1951.842 Y=1346.171 Z=0.000 TraceEnd:X=1951.842 Y=1346.171 Z=-200.000 PenetrationDepth:0.000000 Item:-1 Ph
ysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=48.59, Y=-2.01)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598364 Location:X=48.585 Y=-2.014 Z=-119.673 ImpactPoint:X=48.585 Y=-2.014 Z=-119.673 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=48.585 Y=-2.014 Z=0.000 TraceEnd:X=48.585 Y=-2.014 Z=-200.000 PenetrationDepth:0.000000 Item:-1 PhysMaterial:None 
Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=3159.40, Y=-1182.84)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598179 Location:X=3159.398 Y=-1182.836 Z=-119.636 ImpactPoint:X=3159.398 Y=-1182.836 Z=-119.636 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=3159.398 Y=-1182.836 Z=0.000 TraceEnd:X=3159.398 Y=-1182.836 Z=-200.000 PenetrationDepth:0.000000 Item:-
1 PhysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogActor: Warning: BP_Gun_C /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.BP_Gun_C_0 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
LogActor: Warning: BP_Gun_C /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.BP_Gun_C_1 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
LogActor: Warning: BP_Gun_C /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.BP_Gun_C_2 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
PIE: Play in editor start time for /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap -0.116
LogBlueprintUserMessages: Late PlayInEditor Detection: Level '/Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel' has LevelScriptBlueprint '/Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.FirstPersonExampleMap' with GeneratedClass '/Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap_
C' with ClassGeneratedBy '/Game/Dynamic/Levels/FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.FirstPersonExampleMap'
LogTemp: Warning: SpawnPoint: V(X=662.86, Y=-909.76)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598327 Location:X=662.862 Y=-909.757 Z=-119.665 ImpactPoint:X=662.862 Y=-909.757 Z=-119.665 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=662.862 Y=-909.757 Z=0.000 TraceEnd:X=662.862 Y=-909.757 Z=-200.000 PenetrationDepth:0.000000 Item:-1 PhysMa
terial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=2153.02, Y=-408.03)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598239 Location:X=2153.020 Y=-408.032 Z=-119.648 ImpactPoint:X=2153.020 Y=-408.032 Z=-119.648 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=2153.020 Y=-408.032 Z=0.000 TraceEnd:X=2153.020 Y=-408.032 Z=-200.000 PenetrationDepth:0.000000 Item:-1 Ph
ysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=1354.78, Y=-90.64)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598286 Location:X=1354.778 Y=-90.640 Z=-119.657 ImpactPoint:X=1354.778 Y=-90.640 Z=-119.657 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=1354.778 Y=-90.640 Z=0.000 TraceEnd:X=1354.778 Y=-90.640 Z=-200.000 PenetrationDepth:0.000000 Item:-1 PhysMa
terial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=2859.34, Y=-1240.70)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598197 Location:X=2859.340 Y=-1240.699 Z=-119.639 ImpactPoint:X=2859.340 Y=-1240.700 Z=-119.639 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=2859.340 Y=-1240.699 Z=0.000 TraceEnd:X=2859.340 Y=-1240.699 Z=-200.000 PenetrationDepth:0.000000 Item:-
1 PhysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=1970.64, Y=-1458.11)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598249 Location:X=1970.641 Y=-1458.113 Z=-119.650 ImpactPoint:X=1970.641 Y=-1458.113 Z=-119.650 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=1970.641 Y=-1458.113 Z=0.000 TraceEnd:X=1970.641 Y=-1458.113 Z=-200.000 PenetrationDepth:0.000000 Item:-
1 PhysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=2215.03, Y=-51.45)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598235 Location:X=2215.033 Y=-51.454 Z=-119.647 ImpactPoint:X=2215.033 Y=-51.454 Z=-119.647 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=2215.033 Y=-51.454 Z=0.000 TraceEnd:X=2215.033 Y=-51.454 Z=-200.000 PenetrationDepth:0.000000 Item:-1 PhysMa
terial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=2649.37, Y=-935.15)
LogTemp: Warning: HitPoint: bBlockingHit:True bStartPenetrating:False Time:0.598209 Location:X=2649.373 Y=-935.148 Z=-119.642 ImpactPoint:X=2649.373 Y=-935.148 Z=-119.642 Normal:X=-0.000 Y=0.000 Z=1.000 ImpactNormal:X=-0.000 Y=0.000 Z=1.000 TraceStart:X=2649.373 Y=-935.148 Z=0.000 TraceEnd:X=2649.373 Y=-935.148 Z=-200.000 PenetrationDepth:0.000000 Item:-1 Ph
ysMaterial:None Actor:BP_Tile_C_0 Component:Ground BoneName:None FaceIndex:-1
LogTemp: Warning: SpawnPoint: V(X=465.96, Y=-740.93)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=465.957 Y=-740.928 Z=0.000 TraceEnd:X=465.957 Y=-740.928 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:
None Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=3123.14, Y=1937.38)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=3123.142 Y=1937.376 Z=0.000 TraceEnd:X=3123.142 Y=1937.376 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Acto
r:None Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=2956.39, Y=-1965.33)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=2956.389 Y=-1965.331 Z=0.000 TraceEnd:X=2956.389 Y=-1965.331 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Ac
tor:None Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=1988.34, Y=-66.47)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=1988.342 Y=-66.469 Z=0.000 TraceEnd:X=1988.342 Y=-66.469 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:
None Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=1695.00, Y=-1685.78)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=1694.998 Y=-1685.781 Z=0.000 TraceEnd:X=1694.998 Y=-1685.781 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Ac
tor:None Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=166.02, Y=-1263.53)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=166.021 Y=-1263.527 Z=0.000 TraceEnd:X=166.021 Y=-1263.527 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Acto
r:None Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=3166.72, Y=928.19)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=3166.723 Y=928.190 Z=0.000 TraceEnd:X=3166.723 Y=928.190 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:
None Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=1285.56, Y=415.72)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=1285.562 Y=415.723 Z=0.000 TraceEnd:X=1285.562 Y=415.723 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:
None Component:None BoneName:None FaceIndex:0
LogSlate: FSceneViewport::OnFocusLost() reason 0

And obviously when taking a look at the DebugLines all of them are being drawn in the original spot of the Tile and not the previous one:


I imagine when it tries to detect a hit in a location where the Tile has already been destroyed would return an invalid result

Considering all of this is the reason why I opted to use the World Space coords to place the props onto the Tile ground the LineTrace hit. It would actually be great know if there is a way to utilize LineTraceSingleByChannel that basically follows the relative coordinates of an Actor object.

I am not quite sure what you mean by this, but when trying to use ActorLineTraceSingle() it never returns True for whatever reason (no matter what I adjust in its parameters)

Output:

LogWorld: Bringing up level for play took: 0.003901
LogTemp: Warning: SpawnPoint: V(X=3303.45, Y=651.45)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=3303.446 Y=651.448 Z=0.000 TraceEnd:X=0.000 Y=0.000 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:None 
Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=1427.05, Y=1887.45)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=1427.045 Y=1887.448 Z=0.000 TraceEnd:X=0.000 Y=0.000 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:None
 Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=2604.45, Y=-510.33)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=2604.449 Y=-510.331 Z=0.000 TraceEnd:X=0.000 Y=0.000 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:None
 Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=590.23, Y=-1281.96)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=590.228 Y=-1281.960 Z=0.000 TraceEnd:X=0.000 Y=0.000 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:None
 Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=3830.56, Y=639.24)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=3830.561 Y=639.241 Z=0.000 TraceEnd:X=4000.000 Y=0.000 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:No
ne Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=3952.15, Y=712.49)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=3952.147 Y=712.485 Z=0.000 TraceEnd:X=4000.000 Y=0.000 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:No
ne Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=2243.35, Y=412.43)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=2243.354 Y=412.427 Z=0.000 TraceEnd:X=4000.000 Y=0.000 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:No
ne Component:None BoneName:None FaceIndex:0
LogTemp: Warning: SpawnPoint: V(X=3311.01, Y=-527.67)
LogTemp: Warning: HitPoint: bBlockingHit:False bStartPenetrating:False Time:1.000000 Location:X=0.000 Y=0.000 Z=0.000 ImpactPoint:X=0.000 Y=0.000 Z=0.000 Normal:X=0.000 Y=0.000 Z=0.000 ImpactNormal:X=0.000 Y=0.000 Z=0.000 TraceStart:X=3311.014 Y=-527.665 Z=0.000 TraceEnd:X=4000.000 Y=0.000 Z=-200.000 PenetrationDepth:0.000000 Item:0 PhysMaterial:None Actor:N
one Component:None BoneName:None FaceIndex:0
LogActor: Warning: BP_Gun_C /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.BP_Gun_C_0 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
LogActor: Warning: BP_Gun_C /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.BP_Gun_C_1 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
LogActor: Warning: BP_Gun_C /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.BP_Gun_C_2 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
PIE: Play in editor start time for /Game/Dynamic/Levels/UEDPIE_0_FirstPersonExampleMap -0.689

As in it traces the actor itself. From the docs

Trace a ray against the Components of this Actor and return the first blocking hit

Totally forgot about that aspect, my bad. This should fix it.

FVector Min(0, -2000, 0);
Min = GetActorTransform().TransformPosition(Min);
FVector Max(4000, 2000, 0);
Max = GetActorTransform().TransformPosition(Max);

Or since it’s always a set distance away you could just calculate it which might be marginally better perf-wise. i.e. X * TileNumber

Hmm I see, so I guess the short story is that this is not the time to use this feature.

Oh I see, so instead of what I did of grabbing the world transform of the initial spawn location of the prop, with your suggestion we are grabbing the world transform of the Tile actor itself from the get-go.

I suppose that means there really isn’t a way to implement something like LineTraceSingleByChannel via local transform coordinates in these scenarios…

here is how i implemented your suggestion by the way:

void ATile::PlaceActors(TSubclassOf<AActor> ToSpawn, int MinSpawn, int MaxSpawn)
{
    FVector Min = GetActorTransform().TransformPosition(FVector(0, -2000, 0));
    FVector Max = GetActorTransform().TransformPosition(FVector(4000, 2000, 0));
    FBox Bounds(Min, Max);
    int NumberToSpawn = FMath::RandRange(MinSpawn, MaxSpawn);
    for (size_t i = 0; i < NumberToSpawn; i++)
    {
        FVector SpawnPoint = FMath::RandPointInBox(Bounds);
        UE_LOG(LogTemp, Warning, TEXT("SpawnPoint: %s"), *SpawnPoint.ToCompactString());
        AActor* Spawned = GetWorld()->SpawnActor<AActor>(ToSpawn);
        Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
        FVector EndTrace = SpawnPoint - FVector(0, 0, 200);
        FHitResult hitResult;
        GetWorld()->LineTraceSingleByChannel(hitResult, SpawnPoint, EndTrace, ECC_Visibility);
        UE_LOG(LogTemp, Warning, TEXT("HitPoint: %s"), *hitResult.ToString());
        Spawned->SetActorLocation(hitResult.Location);
        SpawnedActors.Add(Spawned);
    }
}

Well as stated you can just spawn in the correct position instead of moving after spawning.
Here’s how I would do it.

const FVector Min = GetActorTransform().TransformPosition(FVector(0, -2000, 0));
const FVector Max = GetActorTransform().TransformPosition(FVector(4000, 2000, 0));
const FBox Bounds(Min, Max);

const int32 NumberToSpawn = FMath::RandRange(MinSpawn, MaxSpawn);
    
for (int32 i = 0; i < NumberToSpawn; i++)
{
    const FVector Start = FMath::RandPointInBox(Bounds);
    const FVector End = Start + FVector::DownVector * 200.f;
        
    FHitResult HitResult;
    bool bSuccess = GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Visibility);

    if (bSuccess)
    {
        AActor* Spawned = GetWorld()->SpawnActor<AActor>(ToSpawn, HitResult.Location, FRotator::ZeroRotator);
        SpawnedActors.Add(Spawned);
    }
}

Ah I see, you are using the parameters in GetWorld()->SpawnActor() to establish the actual spawn location of the AActor (kind of like what is documented here: https://docs.unrealengine.com/en-US/API/Runtime/Engine/Engine/UWorld/SpawnActor/4/index.html)?

And FVector::DownVector is super useful to know. When I was looking for “how line trace straight down an actor” there was absolutely no mention of this member. Thank you very much!

The constants are at the bottom here.

https://docs.unrealengine.com/en-US/API/Runtime/Core/Math/FVector/index.html#constants

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms