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…