How to orient feet based on the surface underneath's normal? (footplanting)

How would I go about orienting the feet for my footplanting to face forwards (in the same direction the rootpart is facing) as well as lining up with the surface below?

Currently I am making the feet face forwards (in the same direction the rootpart is facing) and I have the surfacenormal of the surface below already. All I need help with is just how to combine these two to get the result I explained above.

The up vector of the cframe would obviously be the normal of the surface. The forward and side vectors can be found by crossing this surface normal and the root part’s look vector. So the original look vector would be the humanoid root parts look vector. Since technically CFrames take the back vector we will actually use the negative of that vector.

local upVec = surfnorm -- however you are getting it
local backVec = -rootPart.CFrame.LookVector

then we get the right vector by crossing the up vector with the back vector

local rightVec = upVec:Cross(backVec)

Since CFrame.fromMatrix can calculate the correct(orthogonal) backvector given the right and up vector we can just feed it the right and up vector.

local cf = CFrame.fromMatrix(hitpos,rightVec,upVec) * CFrame.new(0,legheight/2,0)
-- i assume your already doing this but you would position the leg at the raycast hit point and then move it up by the leg height.
6 Likes

Thanks a lot! I could not wrap my head around this. :smile:

Thank you sir.