Mantids have poor depth perception, and therefore they don't realize that the walls or other distant objects are too far away for them to climb onto. This is part of the reason why mantids also swing back and forth. By swinging back and forth, they see something from different angles and this gives them a better idea of how far away the object is.
It is partly the air moving around them that provokes them to reach out. Also, they sway in the wind to avoid detection from predators, prey, and other creatures.