I know you can use the time.timescale to an extent but i’m sure thats not all maybe speed up animations. Whats the best approach to get a slow motion effect like in the game dishonored when you activate the ability to bend time if you have played it
3 Answers
3You can use Time.timeScale, but for things that will move normally even if time is slow down, you will need to include a different variable for them for their time-based movement.
public class TimeFabric {
private static float timeScale;
public static void SetTimeScale( float ts ) {
Time.timeScale = ts;
timeScale = ts;
}
public static float DeltaTime( bool affectBySlowTime ) {
return Time.deltaTime / (affectBySlowTime)? 1f : timeScale;
}
public static float DeltaTime() {
return Time.deltaTime;
}
}
So, you will use TimeFabric to control the timeScale. TimeFabric.DeltaTime() is used as replacement for all gameObject that depends on Time.deltaTime.
For example, you have 2 script, one for player movement, another for an arrow. When you move your player forward by pressing W, you will use something like this:
player.position.Translate(Vector3.forward *speed *TimeFabric.DeltaTime( false ));
When the arrow is shot, and it start moving, it will do something like this:
arrow.position.Translate(Vector3.forward *speed * TimeFabric.DeltaTime() );
What happen now is that your player will move normally even if the time is slow down, but the arrows will be affected by the time.
Please take note that this will not work properly with TimeFabric.timeScale = 0, if you want to stop time completely, you will need to write a more complex class; I also did not take in consideration on reversing the flow of time here, it is very complex, and a simple script will not be able to pull it off.
How about this:
static var bendTime : boolean;
function Update(){
if(bendTime == true){
Time.timeScale = 0.8;
yield WaitForSeconds(0.6);
Time.timeScale = 0.6;
yield WaitForSeconds(0.4);
Time.timeScale = 0.4;
yield WaitForSeconds(0.2);
Time.timeScale = 0.3;
yield WaitForSeconds(0.2);
Time.timeScale = 0.6;
yield WaitForSeconds(0.5);
Time.timeScale = 0.8;
yield WaitForSeconds(0.5);
Time.timeScale = 1;
}
}
Then somewhere else have something triggering the boolean
as it’s a global variable .
Hope it helped ![]()
Johnny
@ Silan yeah you gave me some ideas thanks ! helps writing up a logic before coding and putting it together
– Charles_Gams@ Sillan Oh wow i had to comment on this again after coming up with a solution i can apply the yielf wait for seconds example you gave into my code like a limit to how long you can use the ability thanks !
– Charles_Gams
If you use physics, don't forget to update the fixed timescale as well... Time.fixedDeltaTime = 0.02 * Time.timeScale;
– almostsyncThanks for the addon. However, what does the 0.02 stands for, time for 1 frame?
– Chronos-Lexactly, the fixed timestep, as set in the player settings/time, is the delay with which the physics do their magic, and the FixedUpdate() calls are propagated to your gameobjects, it should be shorter than the framerate.
– almostsyncYeah i thought it was quiet a complex script if I'm going to attempt this i need to write down the logic in english and translate that into code before i attempt do something like this. But the example code makes sense right now i need type a logic before attempting coding this. If i'm successful then i will let you lot know I'm going to give myself a time limit of 2days max since i'm working on a close combat system. Thanks for the input !
– Charles_Gamsand slowing down the speed to add
– Charles_Gams