The ability to optically excite or silence specific cells using optogenetics has become a powerful tool to interrogate the nervous system. Optogenetic experiments in small organisms have mostly been performed using whole-field illumination and genetic targeting, but these strategies do not always provide adequate cellular specificity. Targeted illumination can be a valuable alternative but it has only been shown in motionless animals without the ability to observe behavior output. We present a real-time, multimodal illumination technology that allows both tracking and recording the behavior of freely moving C. elegans while stimulating specific cells that express channelrhodopsin-2 or MAC. We used this system to optically manipulate nodes in the C. elegans touch circuit and study the roles of sensory and command neurons and the ultimate behavioral output. This technology enhances our ability to control, alter, observe and investigate how neurons, muscles and circuits ultimately produce behavior in animals using optogenetics.