Keep physics with Time.timeScale Time.fixedDeltaTime

Hello everyone,

I’ve just downloaded Unity Free for the first time this week, so I’m pretty new at it, although I’ve been coding for several years.

My goal is to simply slow the time of my game when I hold down a certain key. I’ve achieved this by setting Time.timeScale and Time.fixedDeltaTime and it works wonderfully.

The problem I’m having is that when I do this, the physics in the game to wonky.

For example: in my game, you can throw a ball. Before the change in timescale, say it goes 10 units. After I slow the timescale, it only goes 2 units.

Is there an easy fix for this?

You don’t need to change fixedDeltaTime, I’m pretty sure that this will take timeScale into account on its own.

Nah you are supposed to change fixed time

Are your physics interactions being multiplied by deltaTime (they should be)

No, they’re not. That’s probably it.

So since I’m using addForce to propel it, that’s what I need to multiply by deltaTime?

Ok, so I tried multiplying it by Time.deltaTime and the instance is no longer propelled at all. It just falls.

Here’s my code:

void FixedUpdate ()
{
if(Input.GetButtonDown(“Fire1”)){
Rigidbody projectileInstance;
Vector3 forceDirection = origin.forward;
forceDirection = new Vector3(0, yAngle); //set direction to forward w/ y as well
forceDirection.z = 1;

projectileInstance = Instantiate(projectile, origin.position, origin.rotation) as Rigidbody;
projectileInstance.AddRelativeForce(forceDirection * force * Time.deltaTime);
projectileInstance.AddRelativeTorque(new Vector3(-1f,0,0) * force);
}
}

Ah my bad, deltaTime is for keeping things framerate independant.

Instead, it looks like you need to multiply the added force by 1/timescale

object.rigidbody.AddForce( new Vector3( 300, 300, 0 ) * (1 / Time.timeScale) );

Looks like that worked! Thanks!

Could you explain why I needed to do that?

I can’t answer that specifically (hopefully someone else will :)) but thought I would say, whenever I have trouble understanding math issues like this in my code, I find it super helpful to debug the method - set a breakpoint at the beginning, then step through the lines, checking the values of the numbers as they are implemented. My maths skills aren’t all that great, this really helps to break down the steps so you can observe the behavior more closely