BACKGROUND: Automated delivery of psychological therapy using immersive technologies such as virtual reality (VR) might greatly increase the availability of effective help for patients. We aimed to evaluate the efficacy of an automated VR cognitive therapy (gameChange) to treat avoidance and distress in patients with psychosis, and to analyse how and in whom it might work. METHODS: We did a parallel-group, single-blind, randomised, controlled trial across nine National Health Service trusts in England. Eligible patients were aged 16 years or older, with a clinical diagnosis of a schizophrenia spectrum disorder or an affective diagnosis with psychotic symptoms, and had self-reported difficulties going outside due to anxiety. Patients were randomly assigned (1:1) to either gameChange VR therapy plus usual care or usual care alone, using a permuted blocks algorithm with randomly varying block size, stratified by study site and service type. gameChange VR therapy was provided in approximately six sessions over 6 weeks. Trial assessors were masked to group allocation. Outcomes were assessed at 0, 6 (primary endpoint), and 26 weeks after randomisation. The primary outcome was avoidance of, and distress in, everyday situations, assessed using the self-reported Oxford Agoraphobic Avoidance Scale (O-AS). Outcome analyses were done in the intention-to-treat population (ie, all participants who were assigned to a study group for whom data were available). We performed planned mediation and moderation analyses to test the effects of gameChange VR therapy when added to usual care. This trial is registered with the ISRCTN registry, 17308399. FINDINGS: Between July 25, 2019, and May 7, 2021 (with a pause in recruitment from March 16, 2020, to Sept 14, 2020, due to COVID-19 pandemic restrictions), 551 patients were assessed for eligibility and 346 were enrolled. 231 (67%) patients were men and 111 (32%) were women, 294 (85%) were White, and the mean age was 37·2 years (SD 12·5). 174 patients were randomly assigned to the gameChange VR therapy group and 172 to the usual care alone group. Compared with the usual care alone group, the gameChange VR therapy group had significant reductions in agoraphobic avoidance (O-AS adjusted mean difference -0·47, 95% CI -0·88 to -0·06; n=320; Cohen's d -0·18; p=0·026) and distress (-4·33, -7·78 to -0·87; n=322; -0·26; p=0·014) at 6 weeks. Reductions in threat cognitions and within-situation defence behaviours mediated treatment outcomes. The greater the severity of anxious fears and avoidance, the greater the treatment benefits. There was no significant difference in the occurrence of serious adverse events between the gameChange VR therapy group (12 events in nine patients) and the usual care alone group (eight events in seven patients; p=0·37). INTERPRETATION: Automated VR therapy led to significant reductions in anxious avoidance of, and distress in, everyday situations compared with usual care alone. The mediation analysis indicated that the VR therapy worked in accordance with the cognitive model by reducing anxious thoughts and associated protective behaviours. The moderation analysis indicated that the VR therapy particularly benefited patients with severe agoraphobic avoidance, such as not being able to leave the home unaccompanied. gameChange VR therapy has the potential to increase the provision of effective psychological therapy for psychosis, particularly for patients who find it difficult to leave their home, visit local amenities, or use public transport. FUNDING: National Institute of Health Research Invention for Innovation programme, National Institute of Health Research Oxford Health Biomedical Research Centre.