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.
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);
}
}
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