Short Lag when switching cameras (enable/disable)

Hi.

I have a problem in my scene: When switching between cameras by enabling one and disabling another there is a small, but noticeable lag in the game (about half a second).

There are three cameras (Default, Zoom, Underwater). What’s the best way of switching between them in an FPS game?

Here is my Code:

public enum CameraType {Default, Zoom, Underwater};	
private CameraType activeCamera = CameraType.Default;	

(...)

private void ToggleCamera(CameraType newCamera)
	{
		switch(newCamera)
		{
			case CameraType.Default:
				activeCamera = newCamera;
				defaultCamera.enabled = true;
				zoomCamera.enabled = false;
				underwaterCamera.enabled = false;
				break;
			case CameraType.Zoom:
				activeCamera = newCamera;
				zoomCamera.enabled = true;
				defaultCamera.enabled = false;
				underwaterCamera.enabled = false;
				break;
			case CameraType.Underwater:
				activeCamera = newCamera;
				underwaterCamera.enabled = true;
				defaultCamera.enabled = false;
				zoomCamera.enabled = false;
				break;
		}
}

There are no Errors or Warnings from the Code, everything works except for that annoying freeze/lag when ToggleCamera(…) is called.

I know that i could put all ImageEffect-Components on one Camera and activate/deactivate them as needed but there is bound to be a better/cleaner way…right?

Edit: My Unity Version is 5.2.1f1

Thanks for any advice!

Yours,
Martin

The problem still persists … anyone?

This may not be what you’re experiencing, but it also might be.

My issue (which I would’ve described similarly to yours) turned out to be caused by terrain detail. Specifically, I could see in the profiler that the spikes were caused by Terrain.Data.BuildPatchMesh. This occurred in Water.OnWillRenderObject() where it was taking 550 ms, and in Terrain.Details.Render, where it was taking 280 ms, overall accounting for 98% of the total.

Unfortunately, this is just an issue with Unity. There’s a forum post about it here,
and an issue tracker post here. To get around this issue, you could try setting Terrain.collectDetailPatches to false if your computer has enough memory. Alternatively, decreasing the detail distance and density will reduce the spikes you’re seeing.

Neither of these workarounds are particularly great, but that’s what we have to work with for now.