Why do hats sometimes randomly appear?

A couple of day's ago a YouTuber called "Muselk" uploaded a Team Fortress 2 video where around the 1 minute 30 seconds mark you can see a random hat just 'fly' out of his character.


I have had similar experiences before but mainly shrugged it off as a 'feature'.
What exactly causes this to happen?

Solution 1:

Some backstory on the related feature first: they patched the game to make it so that certain hats "fall off" (some stay on the newly created ragdoll's head) upon a player death.

As Valve does when updating TF2, they broke this purely cosmetic feature, then fixed it, then broke it again (this was around 2013.)

My experience: Having played TF2 since release, I've seen exactly what you are talking about as a hat flying across the map when a player is killed by an explosion/other high impact method. Much like in the video you posted, sometimes client-side or server-side lag/delay will cause the hat from a just killed player to fly out of the ground.

Ironically, a Steam Community post on this exact phenomenon even references being an Engie and seeing it often... Probably due to the opportunity to be more observant than most when playing as Engie! :P

The short of it: This "bug" is almost certainly lag related. It has not been (as far as I know) an officially documented glitch and due to it happening only due to lag, it probably never will.