The concept is pretty simple
You have 3 raycast.
Ray1 is at top of head going forward
Ray2 is at chest going forward
Ray3 is as high as Ray1, but a bit forward i front of body a few pixels, going down
They work like this
If Ray1 doesn't collide but Ray2 collides with something, then you are close to a ledge and should enter hanging state
Then you calculate the distance with Ray3 and correct your hanging position