Laws help solve collective action problems

Sometimes, people want to do something that seems silly to their peers. An example is the people who wanted to start social distancing early on in the COVID-19 crisis.

In these cases, people might accept 'I can't come to your party because I don't want to break the law' more readily than 'I can't come to your party because I'm starting social distancing'. The latter can lead to a debate about whether social distancing is even necessary, or even to being mocked for panicking. The former provides some cover it doesn't reveal whether the person agrees or disagrees with the policy of social distancing itself they just don't want to break the law, which most people think is pretty understandable.

I think the age of consent can work in a similar way someone who is underage and feeling pressured to have sex before they are ready can claim that they don't want to break the law, without revealing their true feelings and risking being called a prude.

(In some cases of course, people will still be mocked for caring too much about the law, but this seems like a less vulnerable position to take)

Sometimes, many people secretly feel the same, but don't realize it because they lack common knowledge, and no one wants to be the one to admit it in the face of social pressure. But when a law is created, and the behavior becomes normalized, it feels safer to admit you actually do agree with the content of the law.