Running Upside-down changes the direction of character's x axis

So I’m working on finishing this “Sonic Infinity Engine”, which has the ground works for a working modern Sonic game. I managed to get the camera to stay behind the character even when they move up or down meaning that the camera always is looking at sonic from behind even running down hills and going up walls. Now with this it’s allowed me to run in a full 360 degrees and effectively go up loop-de-loops which is great for a sonic game. However, when I run forwards through a pipe mesh that I imported from 3DS max with very accurate collision (so it’s not a collision problem) and move, let’s say, right, Sonic will move right and run along the walls but as soon as he gets upside down whilst still holding right it starts to move Sonic left which is something I don’t want if I want to turn all the way around the loop. Vice versa, when I move left instead, once Sonic is upside down he starts to move right.

How do I fix this?