How to rotate a component to match the grounds normal vector

Hi, I started UE4 a bit over a month ago and have tried working on various things. For my current project I am trying to make a movement controller for a physics simulating actor. As it stands I’ve got it moving around and the camera controls and everything else working but I can’t solve one key issue.

I made the actor float a bit, if it hits the ground hard it’ll smash into it so it’s not a constant, but somewhat smooth. The problem is that this means it’s not always in contact with the ground and so can rotate to some odd angles. I have been trying to find a way to make it orient itself along its local X and Y axis of rotation to match the surface bellow it so that when I’m moving over a flat plane it will level out, and when moving over a slope it will angle to match the slope it’s over.

I’ve spent hoursl trying to use a line trace to what’s bellow it and trying to figure out how to convert the on hit normal into a usable rotator I could plug into an add local rotation node or something (preferably lerped to 0 by .95 or something so it’s a smooth transition over several ticks) but I haven’t been having any luck. I searched around a bit but coudn’t find any answers that worked properly on something that actively moves in 3 dimensions and not just one or two so I figured I’d just go ahead an ask. If anyone can help me out it would be greatly appreciated, and thanks in advance for your time, .

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

Get components up vector.

Get a rotation axis of the cross product of the hit surfaces impact normal and… my components up vector?

Normalize the rotation axis.

And… Around here is where I stop being able to really follow that.

I’m using the blueprints UI and not c++, and I haven’t coded in ages so my ability to pick that apart is a bit limited. I’ll try to look into it when I get up tomorrow, but if you could post it in a more… conventionally written instruction set, that would help a lot.

Thanks for your time and help so far though!