Some legal systems attempt to account for this. E.g. England and Wales have two key ages. Having sex with someone under 16 might be (to use US terms) rape or statutory rape depending on whether the person under 16 consented. Both are illegal but rape more so. Having sex with someone under 13 is always rape since children under 13 are not considered able to consent. But in some cases 16 is not the cutoff if e.g. a teacher has sex with a pupil under 18 this is also always illegal. On the other hand if the defendant genuinely did not know the chief witness was under 16 and there was consent this is a defense.