Implemented range checking to ensure that no walls are hit.