How do you set foot IK to terrain height?

I am trying to do some IK on a characters foot so that the foot’s rotation is equal to the terrain below it. How would I do this using raycast (so I get the correct angle directly below the foot)? It should also be independent of the foot’s local or global rotation.

TerrainData.GetSteepness