RAW is unclear
The Underwater Combat rules on page 198 of the Player's Handbook are very short and don't give a great ruling on what counts as underwater combat. They state:
Underwater the following rules apply.
These rules seem to assume that both the attacker and the target are underwater. They also assume the line of attack is underwater for it's entire length.
As there is no exact ruling to cover the condition where a creature is attacking into or out of water the only correct answer is "up to your DM".
How I would rule it
Here is how I see it and would rule as a DM however.
A ranged weapon attack automatically misses a target beyond the weapon's normal range. Even against a target within normal range, the attack roll has disadvantage unless the weapon is a crossbow, a net, or a weapon that is thrown like a javelin (including a spear, trident, or dart).
Ranged weapon attacks miss beyond their normal range due to the drag of the water. If the attack is in the air for the majority of its flight and only enters the water immediately before striking the target this restriction doesn't make sense. The same reasoning apply for why ranged attacks have disadvantage underwater.
Overall this is a very niche ruling that, unless you are in the habit of fighting sea-monsters, is unlikely to come up more than once or twice in a campaign. I suggest you pick a rule when it comes up in combat and stick to it. So long as you apply it consistently it won't make a major difference.
With this logic I can see three possible logical rulings:
- A ranged attack uses the underwater combat rules if the attacker is underwater
- A ranged attack uses the underwater combat rules if more than half its range in underwater
- A ranged attack uses the underwater combat rules if more X feet of its path are underwater.
Of the first two, I am not convinced either way which is a better rule and have played with both. This situation is rare enough that it has only come up once or twice with each rule and I haven't noticed a major difference. I have never used the third rule but may implement it the next time this situation comes up.
When making a melee weapon attack, a creature that doesn't have a swimming speed (either natural or granted by magic) has disadvantage on the attack roll unless the weapon is a dagger, javelin, shortsword, spear, or trident.
Similar to ranged weapons it is the drag of the weapons while underwater that imposes disadvantage. That is why a subset of piercing weapons are exempt, they are the types of weapon that would travel well underwater. Similar to ranged attacks I can think of three possible rulings for this:
- A melee attack uses the underwater combat rules if the attacker is underwater
This rule is the minimum in my opinion, melee attacks take a back swing and movement of your body. When this is entirely underwater the underwater combat rules should apply.
- A melee attack uses the underwater combat rules if it requires reach of more than 5 feet through water
This rule can either be used together with the first or separately. This handles the case of creatures being under 1" of water where it wouldn't make sense to have disadvantage.
- A melee attack uses the underwater combat rules if either the attacker or the target are submerged underwater
This rule would apply the most often and is probably the most correct reading of RAW. Anytime a melee attack passes through water it is made at disadvantage.
I would have no issue applying any of these at my table. I have used the first and third rulings and encountered no issues. The situation where the second ruling would apply but not the first has never come up at my table, though I believe it would also be balanced.