How to rotate a component to match the grounds normal vector

Component rotation over the grounds normal vector

const FVector UpVector = Component->GetUpVector();
FVector RotationAxis = FVector::CrossProduct(UpVector, HitSurface.ImpactNormal);
RotationAxis.Normalize();

float RotationAngleRad = acosf(FVector::DotProduct(UpVector, HitSurface.ImpactNormal));
FQuat Quat = FQuat(RotationAxis, RotationAngleRad);
FQuat NewQuat = Quat * Component->GetComponentQuat();
FRotator NewRotator = NewQuat.Rotator();
NewRotator.Yaw = 0;
Component->SetRelativeRotation(NewRotator);
1 Like