Rashi to Genesis 7:4 explains that it's because of their sin of adultery:
ארבעים יום. כְּנֶגֶד יְצִירַת הַוָּלָד, שֶׁקִּלְקְלוּ לְהַטְרִיחַ לְיוֹצְרָם לָצוּר צוּרַת מַמְזֵרִים
"Forty days" – corresponding to the formation of the fetus, for their sins troubled their Creator to form the form of children of illicit relations.
Rashi here refers to a ...