[SOLVED] Can you slow down game time?

Is there a way to slow down game time, then speed it back up?

For instance, I want to really juice up a moment where a character lands a final blow and the game slows down to highlight it and showcases the hit for a moment, then back to full game speed.

For this you would typically make your animation speed dependent on delta time. Then it’s just a matter of applying a time scale. Here is a simple example:

MyScript.attributes.add('target', { type: 'vec3' });

MyScript.velocity = new Vec3();

MyScript.prototype.update = function(dt) {
    var timeScale = 1;
    this.move(dt * timeScale);
}

MyScript.prototype.move = function(dt) {
    var velocity = MyScript.velocity;
    
    // Distance we want to move per second in world units
    var speed = 3;
    
    // Get the direction to the target
    velocity.sub2(this.target, this.entity.getPosition());
    
    // The velocity is along this direction, at full speed
    velocity.normalize();
    velocity.scale(speed);
    
    // Update entity's velocity taking into account delta time
    this.entity.rigidbody.linearVelocity.copy(velocity.scale(dt));
}

Normally timeScale would be 1 and if you would want your entity to move in slow motion, say half of normal speed, all you would have to do is change timeScale to 0.5. This is by no means an optimized example but should give you an area to explore for your particular needs.

1 Like

Well also consider a situation where physics is at play.

Would I modifiy the physics to fake slow motion?

For instance if the downward force is -9.8 i’d slow this down too by just changing it to -2.5 or something.

Thoughts?

Or am I know reading this correctly? (which is probably true :slight_smile: )

This is probably what you need: https://developer.playcanvas.com/en/api/pc.Application.html#timeScale

3 Likes

Damn, I didn’t even remember that existed. Thanks for pointing this out.

Sick! Ill scope it out!