Trying to snap to ground procedural mesh at runtime but having weird issues

In my project I’m creating a mesh at runtime and I want it to position it just above the floor. To do that I’m raycasting from up above down to the ground and then I’m positioning the mesh on the impact point, adding to the Z value half the height of the mesh bounding box.

I’m retrieving the mesh bounding box with GetActorBounds(), but I’m having issues with it.

If I position the actor in the world and draw the bounding box using this blueprint:

I get the right result:

If then I try to raycast to the ground and move it there with this code in the BeginPlay() of the actor

// I'm starting the raycast from the origin of the world but up high and shooting down 
FHitResult hitResult;
FVector rayStart(0, 0, 10000);
FVector rayEnd = rayStart + FVector(0, 0, -1) * 100000;
GetWorld()->LineTraceSingleByChannel(hitResult, rayStart, rayEnd, ECollisionChannel::ECC_Visibility);

FVector impactPoint = hitResult.ImpactPoint;

// Getting the bounds
FVector origin;
FVector boundsExtent;
GetActorBounds(false, origin, boundsExtent);

// Moving the actor
SetActorLocation(impactPoint + FVector(0, 0, boundsExtent.Z));

I get the wrong result and the bounding box is totally wrong:

Here it might seem that the mesh is on the ground, but if I load other meshes the results are totally different (they may be up above or down below the floor).

One thing I noticed is that after moving the actor in the new position the bounding box somehow starts in the *old *position:

I have no idea why…

News: I found out that some of the meshes I have been using don’t have as origin the center of the mesh, so adding the height of the bounding box (which is actually computed correctly apparently) was messing everything up. Still, I’ve no idea why the bounding box gets drawn completely wrong, and if someone could explain it to me I’d appreciate it.

So, this brings me to a new question: how can I move objects to the ground indipendently from where their origin is located? The raycast can’t work with this condition, so I need something different

Either you take into account the origin Z offset or you place the origin at the bottom of the model.