It happens, but it's nowhere near as bad as it was at various time in RTW. Generally, you'll only take friendly fire immediately after you engage a unit that has been under fire by ranged attack. Your ranged attackers take a bit of time to pick a new target, so they'll often still loose a volley even if you've engaged their target (in which case they switch targets). You can also take FF casualties by having units very close to a target that is unengaged and being fired upon.

It's pretty easy to prevent them all around, just make sure your ranged attackers stop firing or switch targets before you close to melee range. What you're doing sounds correct.