How to stop objects going through walls?

My character has a sword in its hand but the sword always goes through walls. hwo can I stop this? Thanks for your help.

Use collision detection. If the sword collides with the wall, stop the animation.