I play a lightfoot halfling rogue. An ally of mine, a fighter, is in melee combat with an orc. According to the rules I can move behind the fighter, use my cunning action to hide, and if my stealth roll is higher than the orcs passive perception I can then use my shortbow to make a ranged attack with advantage. I then lose hiding due to the attack. That way I can get advantage every round on my ranged attack, and not just sneak attack, for which I don't need to hide.
Do I understand it right that this only works for ranged attacks? I can't hide if I am in melee range. And if I move from behind the fighter, I lose the hiding advantage before I can make a melee attack.
Is there any way in this situation to make a melee attack on the orc with the advantage from being hidden?