Archers could and did aim at close range targets during sieges and leading an assault was undoubtedly very dangerous, especially close to the wall when troops faced the additional threat of spears and heavy stones being thrown down on top of them. But there were still plenty of methods that could improve survival.
First, unlike the movies, decent mail or plate armor usually protected pretty well against most bows and crossbows (/u/elos_). Attacking troops could also rely on troops with large shields or pavises to lead the way and protect them from projectiles. Even after the bulk of medieval infantry started to be equipped with polearms or other two-handed weapons, shield-bearers or targeteers continued to be a popular choice for leading the assault over over the enemy walls or into a breach. Attackers could also rely on makeshift mobile defenses like mantlets or large shacks on wheels (edit: commonly known as tortoises) to protect sappers or battering rams while they got close enough to damage the enemy defenses.
Last, it's important to remember that attackers were generally shooting back at the defenders as well. Cannons and siege engines definitely made it dangerous for defending archers to stay on top of walls and towers while bowmen and crossbowmen who advanced close to the walls behind thick pavises might be close enough to harass or pick off defenders who leaned to far out over the wall or spent too much time trying to take careful aim at important targets. Archers on top of wooden siege towers might be high enough to shoot at an even level or even down on top of defenders atop the walls. Once armor became less useful due to the proliferation of firearms it started to become more and more important for attackers to gain fire superiority during an assault. In the late 1500s, mercenary Humfrey Barwick described how one fortified town fell in a single day with minimal casualties because the French arquebusiers got so close to the walls that the spanish defenders could not stick their heads over the top to fight back while French engineers blasted their way in.
Hi, hopefully one of the military flairs will drop by to address the specific scenario of firing on attackers in a siege, but meanwhile, you may be interested in a couple of threads addressing the general strategy of firing in volleys
Did those showers of arrows really exist? - featuring /u/MI13
Why was volley fire prefered with muskets and arrows vs. allowing everyone to fire at will? - featuring /u/bigbluepanda