Self-organized multicellular behaviors enable cells to adapt and tolerate stressors to a greater degree than isolated cells. However, whether and how cellular communities alter their collective behaviors adaptively upon exposure to stress is largely unclear. Here, we investigate this question using Bacillus subtilis, a model system for bacterial multicellularity. We discover that, upon exposure to a spatial gradient of kanamycin, swarming bacteria activate matrix genes and transit to biofilms. The initial stage of this transition is underpinned by a stress-induced multilayer formation, emerging from a biophysical mechanism reminiscent of motility-induced phase separation (MIPS). The physical nature of the process suggests that stressors which suppress the expansion of swarms would induce biofilm formation. Indeed, a simple physical barrier also induces a swarm-to-biofilm transition. Based on the gained insight, we propose a strategy of antibiotic treatment to inhibit the transition from swarms to biofilms by targeting the localized phase transition.