Animals continuously gather sensory cues to move towards favourable environments. Efficient goal-directed navigation requires sensory perception and motor commands to be intertwined in a feedback loop, yet the neural substrate underlying this sensorimotor task in the vertebrate brain remains elusive. Here, we combine virtual-reality behavioural assays, volumetric calcium imaging, optogenetic stimulation and circuit modelling to reveal the neural mechanisms through which a zebrafish performs phototaxis, i.e. actively orients towards a light source. Key to this process is a self-oscillating hindbrain population (HBO) that acts as a pacemaker for ocular saccades and controls the orientation of successive swim-bouts. It further integrates visual stimuli in a state-dependent manner, i.e. its response to visual inputs varies with the motor context, a mechanism that manifests itself in the phase-locked entrainment of the HBO by periodic stimuli. A rate model is developed that reproduces our observations and demonstrates how this sensorimotor processing eventually biases the animal trajectory towards bright regions.