The cannons, as mentioned, are not the best on the M2.
I'll describe why:
The cannons on the A6M2 are like the MG/FF on the 109E. They are explosive rounds so when they hit they can kill, but the problem is hitting. The have a slow velocity, so they "drop" quite badly, and they disperse (not overly accurate) in a pattern. So the reason you need to get in close is because the rounds won't go where you need them.
They won't kill if they don't connect.
Taking your "turning with a P-38" example, the 20mm on the A6M2 will drop much further behind a target, requiring much more lead than a plane with guns that fire in a "flat" trajectory. 50cals and hispanos have very flat trajectories, for example, and you need to lead much less. With the 109E4 and A6M2, you need to lead much more than you think. Try turning the lead computing sight on (the green "plus sign" that tells you where to aim) when you're offline or in the TA to figure out how much you need to lead.
Setting the cannons for long convergences can also screw up your aim, since this aims them "up" more. Setting them for "600 yards" then firing at a target "250 yards" away you can easily miss. You can waste ammo, or if your convergence is way off just hit with 1 gun on an unimportant part of the plane (an aileron or something).
So the general reason to get in close isn't because the guns are weak as much as it's because the guns have really low hit % further out.