How to pause (and abort) a coroutine.

So, I have this cinematic sequence where the camera changes state from following the character to entering a camera “path”. It’s mostly used for reveal sequences and such.

It looks a bit like this:

IEnumerator MoveCamera(Cinematic cinematic)
	{
		float timePassed = 0;
		float hermiteLerpFactor;
		mainCamera.camstate = MainCamera.CamState.Cinematic;
		
		while(cinematic.duration - timePassed >= 0)
		{
			// Ease-In / Ease-Out
			hermiteLerpFactor = Mathfx.Hermite(0, 1, timePassed/cinematic.duration);
			mainCamera.gameObject.transform.position = Vector3.Lerp(cinematic.start.position, 
																	 cinematic.end.position, 
																	 hermiteLerpFactor);
			timePassed += Time.deltaTime;
			yield return null;
		}
		yield return new WaitForSeconds(cinematic.totalDuration - cinematic.duration);
		mainCamera.camstate = MainCamera.CamState.Follow;	
	}

So, what I do to skip the cinematic is to set duration and totalduration to 0, and this seems to work okay. The coroutine quickly finish.

However, I mainly wonder how I could safely pause this cinematic sequence (or any other coroutine for that matter).
Also, I wonder if it’s ok to use a coroutine like that…?

Thanks for any help and guidance.

Kjetil

Stop a Coroutine: Unity - Scripting API: MonoBehaviour.StopCoroutine
Start a Coroutine: Unity - Scripting API: MonoBehaviour.StartCoroutine

There isn’t anything about pausing a Coroutine but you could probably build that functionality into your code pretty easily.

while (paused)
{
yield return null;
}

Yep, but stopping it like that would abort everything I guess. But yes, I could move the reset code out of the coroutine. Thanks for the reminder.
And then I could add the paused segment wherever I want the coroutines to be pauseable.
Yeah, I think it makes sense.