Wait for large amounts of code to execute before moving camera to ensure the camera is very smooth

I am doing something similar to old school Zelda, where you walk through a door, generate a new room, then transition/pan the camera over to the new room.

Everything works perfectly, except occationally I get an initial jump in the camera movement. I think (not certain though) that this is because there is a ton of code that needs to be executed, lots of objects to be generated, etc. and I am also simultaneously moving the camera to the center of the new room at this point.

It is crucial that I get a very smooth transition with the camera. It would be acceptable to even have a slight delay to make sure all of the heavy lifting is done prior to starting to move the camera.

Is there a good method for doing this? Is this as simple as putting something like a frame counter at the end of all of that code? For example at the end of the code execution, I set frame counter to 1. On the next frame I increment it again to 2. I then don’t start to move the camera until the frame counter equals 2. If there was a delay and it messed with the frame rate, it would only effect frame 1 and 2, and not the camera movement right? Is this a correct way of doing it or is there a better way?

Thanks and any help or advice is greatly appreciated!

Use the Profiler window to work out what the process is that’s taking a long time to execute. (It’s usually either loading, or the Awake()/Start() of whatever has just loaded) Once you know that, you can set a globally accessible flag that you’re loading slow stuff once you kick off that process, and then set it false after it completes.

1 Like

You could also split your initialization code between several frames, using a Coroutine for example.