The interlayer sliding potential of multilayered hexagonal boron nitride (h-BN) and graphene is investigated using density-functional theory including many-body van der Waals (vdW) interactions. We find that interlayer sliding constraints can be employed to tune the contribution of electrostatic interactions and dispersive forces to the sliding energy profile, ultimately leading to different sliding pathways in these two materials. In this context, vdW interactions are found to contribute more to the interlayer sliding potential of polar h-BN than they do in nonpolar graphene. In particular, the binding energy, the interlayer distance, and the friction force are found to depend sensitively on the number of layers. By comparing with the experimental findings, we identify sliding pathways which rationalize the observed reduced friction for thicker multilayers and provide quantitative explanation for the anisotropy of the friction force.