It depends on the Sonic.
Game Sonic doesn’t strike me as the ‘touchy-feely’ type. If you were upset though and wanted a hug, I definitely think he’d oblige if for the simply belief of his that it’d alleviate your sadness because one of his stated dislikes is sadness.
X Sonic allows hugs. He’s not very fond of Amy’s more glompy ones but he actually allows her to on some occasions. He also lets Chris hug him a few times.
Archie Sonic allows physical intimacy like that, like sitting close and yes, hugging. He tends to look a bit awkward regarding it though.
Fleetway Sonic would shove-away the hugger, give them a death glare and tell them not to do it again in a rather dark tone.