AnyPortrait > Manual > Controlling update rate with scripts

Controlling update rate with scripts


1.3.5

Various directing techniques are used in games, one of which is controlling the playback speed.
You can speed up or down the overall game to provide a tense play, or you can control the animation speed of a specific character for a fun effect.
Basically, you can control the speed of the game using Unity's Time class.
In addition to this, you can control the animation playback speed of AnyPortrait's characters with a script.
This page presents an example script that controls the animation playback speed.


The description of the script function can also be found in the related page.




To show an example of the script, 4 identical characters were ready as above.
The results of the example script below can be seen in the video at the bottom of the page.




Changing the speed of gameplay



All objects using Time.deltaTime are affected by the game speed.
Characters created with AnyPortrait are also affected by the game speed by default.
All the cases in the video below are compared when the overall game speed is 1x and 3x, respectively.




Method 1: Specifying the speed multiplier



SetUpdateTimeMethod(bool useUnscaleDeltaTime, float multiplier) function sets the animation speed by multiplying by a constant speed ratio based on the update rate of the entire game.
- bool useUnscaleDeltaTime : If you enter the default value of false, the game speed (Time.timeScale) will be affected. If you enter true, the game speed is not affected.
- float multiplier : The update time is determined by multiplying the update rate of the entire game by a certain percentage. If not entered, it is multiplied by 1.


If you want to restore the speed ratio to the default, write as "SetUpdateTimeMethod(false)".




Method 2: Using a callback function



The "SetUpdateTimeMethod(OnDeltaTimeRequested onDeltaTimeRequested, object savedObject)" function gives you more flexibility to change the playback speed.
All we need to do is return the game time of the frame in which this function is called, and we can change the return value to make the animation play faster or slower.
The cool thing about this method is that you can change the returned game time value in runtime.
For example, in a game, you can easily express using this function that a character is slowed down for a certain period of time due to "slow magic skill".
Or it's easy to express a gradual change in playback speed.
If you input savedObject, you can identify which character this function is being called for, as shown above.
"Time.deltaTime" can be used to make the game speed affected.
Conversely, you can use "Time.unscaledDeltaTime" to create an object that is not affected by game speed.
It is also possible to implement your own system to create a separate game speed different from Unity's time.


Check out the video below to see the difference in playback speed in each of the above cases.



Note
These above functions have the following limitations.
- It does not apply when using Mecanim. In this case, it follows Mecanim's speed.
- It does not apply to physics features.