How to fast forward time (or just skip it) properly?

Not sure what is exactly going wrong here.

I have a world time that runs on update:

	if (!worldInteractions.worldIsPaused && !worldInteractions.showMainMenu){
		worldTime += Time.deltaTime;
	}

and I use that time to drive the game world clock :

function GameTime () {
	if (worldTime - lastChange > 10) {	//Decides how many seconds a game 'minute' lasts.	
	    minutes++;
	    
	    if (minutes == 60) {			
	        minutes = 0;
	        hour++;
	        if (hour == 12) {
	            if (ampm == "am") {
	                ampm = "pm";
	
	            }
	        }
	        if (hour == 13) {
	            hour = 1;
	        }
	    }
	    lastChange = worldTime;
	}
}

In my project, when the player is in a dialog, he is able to skip a dialog line and also advance the time it would take to display it(e.g. if a line of text would display for 5 seconds and the player skips it, then it will be 5 seconds later).

This is how a dialog is currently displayed (I’m doing a typewriter effect from a script shown in Unity wiki I think):

function DoTextLine (){
	currentDisplayLine = null;

	for (var letter in currentScriptStep.textLine.ToCharArray()) {
		currentDisplayLine += letter;
		yield WaitForSeconds (letterPause);
	}
	
	yield WaitForSeconds (endDialogPause);
	
	currentState = DialogStates.Idle;
}

And when the player presses a key to fast forward that dialog I do this:

function SkipDialogLineAction (){
	if (dialogInteractions.currentState == DialogStates.ShowTextLine){
		Time.timeScale = 90;
	}
}	

and once the textLine has been fully displayed in faster speed I go back to the normal timeScale:

if(currentState == DialogStates.Idle && Time.timeScale == 90){
	Time.timeScale = 1;
}

I thought this was working all correctly but I noticed that time passes much faster than it should.
If I skip a dialog line of 5 secs I end up passing almost 30 secs from the worldTime variable.
I’m wondering what I could be doing wrong. I’m not totally comfortable with Time.time and Time.deltaTime and wondering if there is a error there? Or if maybe using yield on the Typewriter effect could be causing the issues with the timeScale difference…

Or maybe there is a much better way to achieve this?

Any help very appreciated!

When you set ‘Time.timeScale = 90’, time will pass 90 times as fast. This means that Time.deltaTime will also be accelerated by 90x. So if it takes 1/3 of a second to skip the dialog, your worldTime will advance by 30 seconds. I see two possible fixes. First, you could change your letterPause variable to something small to cause the typing to go quickly rather than increasing Time.timeScale. Or you can save and restore worldTime. For the latter I mean something like:

function SkipDialogLineAction (){
    if (dialogInteractions.currentState == DialogStates.ShowTextLine){
       Time.timeScale = 90;
       worldTimeSaved = worldTime;
    }
}    

if(currentState == DialogStates.Idle && Time.timeScale == 90){
    Time.timeScale = 1;
    worldTime = worldTimeSaved + 5.0;

}

In case it helps future people (and feel free to correct it Robertbu!) here is the solution I’m using based on the discussion we had, seems to be working only being about 1 to 2 seconds off.

function SkipDialogLineAction (){
	if (dialogInteractions.currentState == DialogStates.ShowTextLine){
		//Store the current world time and the dialogTimeLeft
		var currentWorldTime = scriptWorld.worldTime;
		var dialogTimeLeft = dialogInteractions.textLineDisplayTimeLeft + dialogInteractions.endDialogPause;
		
		//Cancel current dialog
		dialogInteractions.StopCoroutine("DoTextLine");
		dialogInteractions.currentDisplayLine = null;
		dialogInteractions.currentState = DialogStates.Idle;

		//Advance game time based on how much of the dialog was left
		Time.timeScale = 90;
		yield WaitForSeconds (dialogTimeLeft);
		
		//Bring timescale back to 1 and correct worldTime
		Time.timeScale = 1;
		scriptWorld.worldTime = currentWorldTime + dialogTimeLeft;
	}
}