How can I play music when a scene's camera starts rendering?

I’m helping some game design students make a horror game with the HDRP pipeline in Unity 2022.3.26f

I’ve got a “CoreSystems” scene holding a application manager gameobject that on Start() asynchronously loads the “Main Menu Scene” from an addressables reference.

This menu scene has a camera, some environmental geometry, a global pre-processing volume to give it a haunted appearence, and a world space UI canvas. It also has a script on the camera where on Start() it will invoke the AudioManager’s static PlayMusic action that activates it’s audiosource (this manager being in the core systems scene).

What I’m noticing in the editor on entering play mode (which also correlates to build behaviour) is that the scene will load (going from grey to white text in the heirarchy window), the music will start playing (so start() gets triggered in the scene), but the scene’s camera won’t start rendering until about 3s later (at which time the editor will show a drop down arrow for the scene’s gameobjects).

Not sure which end of the beast is the head so to speak… :sweat_smile:

I originally thought the async scene loading was the issue and tried hooking into SceneManager.SceneLoaded to trigger the music but that get executed between OnEnable and Start.

I then tried researching how to trigger callbacks when a camera starts renders and found out I could hook into BeginCameraRendering but what happened then was music would start to play about half a second before the camera rendered but then abruptly stop. Bug?

Any guidance would be appreciated :slight_smile:

Also here’s a gif to show the camera delay.

HDRPCameraRenderDelayWhenAsyncSceneLoading

Me from the future.

I think my problem was due to HDRP expecting only one camera in the game unless one utilises custom render passes

Since I more familar with URP, I was using seperate cameras for the loading menu UI, the main menu UI, and the player. Whoops.

So the extended black screen time was the delay in the HDRP trying to find the main camera in my loaded scenes.

Am not 100% sure about this answer but swapping my cameras for virtual cinemachine cameras has stopped the lingering black screen.

you should not rely on these “hacks” for gameplay elements. if you need these synchronized events to happen, optionally you can use cinemachine so you can swap between different cameras, then use unity timeline

https://docs.unity3d.com/Packages/com.unity.timeline@1.8/manual/index.html

Am finding using Timeline and Cinemachine together is the recommended HDRP workflow :slight_smile: