Unless you are behind a solid object and there's no physical way for an attack to hit you (say you are around a corner or behind a wall)... you can normally still be attacked while you are unseen, because the target knows approximately where you are (even if they can't see you.) However, that attack is made with Disadvantage.
To be 'Hidden' means you have been masked from ALL the senses of the target. They cannot see you, they cannot hear you, they cannot smell you, they do not notice anything in the environment that indicates where you are. The way you accomplish this is by making a Dexterity (Stealth) check, and your result is compared to the Passive Perception of the target. If your check is higher than their PP, then they not only do not know where you are, they possibly do not even know you are there.
Theoretically they *can* still attack you... but they would do it by just selecting an area or a square where they guess you might be... and make an attack roll (with Disadvantage, because you are also Unseen.) Obviously if you aren't in that square they selected, the attack automatically misses. And if by some chance they just happen to select the correct square you are in... they probably will still miss you because they are rolling with Disadvantage, but there is a chance they could hit you. In addition, if they cast an area of effect spell and you just happened to be within the area the spell goes off in (and there is a "line of effect" between the target space of the spell and where you are), you can also be affected or take damage by the spell.
*****
The thing that often bothers people about these rules is that they do not like that being Unseen doesn't automatically also confer the Hidden status. Some folks think that if you are invisible for example, targets shouldn't know where you are at all. But technically, until you've used your action to try and Hide (by making a Dexterity (Stealth) check)... the game assumption is that even if you are invisible, you are still making noise or you are giving off an odor or the air or objects around you are moving noticeably to indicate your location. Many DMs who do not like that assumption will either let the invisible creature make a Dexterity (Stealth) check automatically as part of being invisible, or they will just say that the invisible creature cannot be noticed or found regardless (all depending on how a particular DM thinks invisibility should work in their game world.)
What you prefer to use is up to you.
ncG1vNJzZmivp6x7prrWqKmlnF6kv6h706GpnpmUqHyltcWfnKudnpiybq7Era6enZ5ir6a1zaBkoaGUmbKvecCnm2aalZ67qHnUp6qenZ5jg3aDkW9qaA%3D%3D