Help with Mech Leg Rotation

Heya, I am trying to get the legs on a mech the player controls to be oriented based on what direction you are moving AND which direction you are facing. I’ve tried a lot of things, but I can’t seem to get the math correct. Note that I am physically rotating the legs and not using an animation (the upper and lower bodies are separate game objects).

An example of this would be the mantis from Halo 4, where the torso rotates independently from the legs. Any insight is appreciated.