I see a lot of “morally ambiguous” characters that fail hard at having any ambiguity in their morals. The author presents a villain with a sad backstory or a hero who breaks the rules as morally ambiguous, when really they’re still only a hero or a villain. There’s nothing wrong with those types of characters, but problems start when the author tries to present them as morally ambiguous.
A morally ambiguous character needs either A) both good and evil motivations or B) an enormous discrepancy between their means and their ends. Let’s check out some examples.
Miko Miyazaki from The Order of the Stick is a Paladin and truly believes in being a hero, doing good, and righting injustices. Unfortunately, she has such a strict set of morals and such harsh punishments that she seems downright evil at times. Even when presented with evidence from the gods that what she’s doing is wrong, she sticks with her stringent morals until she dies. Her means and her motivations are so far apart that though she’s technically “lawful good,” most of the Oots fandom hates her and the actual morality of her actions falls all across the scale.
Vriska Serket from Homestuck has two motivations. On one hand, she wants to save paradox space and win against evil. On the other hand, she wants to be at the center of every major event and for everybody to constantly pay attention to her. Her second motivation cause her to play a part in the creation of one of the main antagonists. You can find thousands of pages of discussion on the internet as to whether she’s a hero or a villain.
Santiago from Nanquest believes he is above good and evil and that he is free to do whatever he wants, including killing people. However, he often helps the main character and he seems overly defensive of his own philosophy. Sometimes he does truly despicable things, but many of them are simply to warn the protagonists of unspeakable danger.
All of these characters do both really, really bad things and really, really good things. They have solid motivations for doing these things, too, not just flimsy little ways for the author to add “depth” where there is none. The best way to make a morally ambiguous character is not to peg them as “heroic” or “villainous” beforehand.