Cockroaches live in a wide range of environments around the world. Pest species adapt readily to a variety of environments, but prefer warm conditions found within buildings. Many tropical species prefer even warmer environments and do not fare well in the average household. Cockroaches are mainly nocturnal and will run away when exposed to light. A peculiar exception is the Asian cockroach, which is attracted to light. The spines on the legs were earlier considered to be sensory, but observations of their locomotion on sand and wire meshes have demonstrated they help in locomotion on difficult terrain. The structures have been used as inspiration for robotic legs.
Cockroaches leave chemical trails in their feces, as well as emitting airborne pheromones for swarming and mating. These chemical trails transmit bacteria onto surfaces.[citation needed] Other cockroaches will follow these trails to discover sources of food and water, and also discover where other cockroaches are hiding. Thus, cockroaches can exhibit emergent behavior, in which group or swarm behavior emerges from a simple set of individual interactions. Daily rhythms may also be regulated by a complex set of hormonal controls of which only a small subset have been understood. In 2005, the role of one of these proteins, pigment dispersing factor (PDF), was isolated and found to be a key mediator in the circadian rhythms of the cockroach.