You'll have to play with the values to make it work. Try setting the objects further apart or changing the distance in your function to something larger.

One way to do this is to set distances to be too large, such that one object will stop too far away from another object. Then set the distance a little closer. Keep lowering this number until it's the lowest value before the object goes through the other.