Synthetic strategies for optically controlling gene expression may enable the precise spatiotemporal control of genes in any combination of cells that cannot be targeted with specific promoters. We develop an improved genetic code expansion system in Caenorhabditis elegans and use it to create a photoactivatable Cre recombinase. We laser-activate Cre in single neurons within a bilaterally symmetric pair to selectively switch on expression of a loxP-controlled optogenetic channel in the targeted neuron. We use the system to dissect, in freely moving animals, the individual contributions of the mechanosensory neurons PLML/PLMR to the C. elegans touch response circuit, revealing distinct and synergistic roles for these neurons. We thus demonstrate how genetic code expansion and optical targeting can be combined to break the symmetry of neuron pairs and dissect behavioural outputs of individual neurons that cannot be genetically targeted.