Create portal with correct rotation

Hi all, please let me know if I don’t explain this very well but I’m making a portal gun where I have made it so it fires a line trace at a wall, cube or whatever and I would then like to spawn my portal on that object. I thought at first this would be fairly simple but say if the wall is slanted at a different yaw rotation, the portal doesn’t spawn on that object with the correct rotation.
Does anyone know how I can make it so my portal has the correct rotation where it looks like it has appeared on that objects surface because I have tried using the actors rotation but that only works with one face of the wall or cube.

Here is my code at the moment and I’m testing with DrawDebugBox instead of spawning my portal for now.



FVector CameraLocation = Camera->GetCameraLocation();
FVector Start = CameraLocation;
FVector End = (Camera->GetActorForwardVector() * MaxLineTraceDistance) + Start;

FHitResult Hit = FHitResult(ForceInit);

bool bIsHit = GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility);

if (bIsHit)
{
FRotator ActorRotation = Hit.GetActor()->GetActorRotation(); 

DrawDebugBox(GetWorld(), Hit.Location, FVector(1.0f, PortalHalfHeight, PortalHalfLength), ActorRotation.Quaternion(), FColor::Purple, true);
}


Thank you, any help is much appreciated!

Use the hit normal to get the correct rotation. It is a unit vector, so you’ll have to make a rotator out of it.

Thanks so much it now works!