- Home /
Time.timeScale is Having a Strange Effect on Physics
Hey all,
I am currently building a 2d physics-based game. I've built a moving character who can shoot bullets (which naturally travel much faster than the character).
Recently I've tried implementing slow-mo into the game by pressing space. The issue I'm having is that it seems to affect the physics-based objects in different and unexpected ways. The main issue is that the bullets become slower relative to the character - the lower the slow-mo timescale, the slower the bullet relative to the character. With a Time.timeScale as low as 0.1, the character can actually travel faster than the bullet and I have no idea why.
I have built a slow-mo script which fades in and out of slow mo by pressing the space bar. Relevant code:
if (Input.GetKeyDown (timeSlow))
{
slowOn = !slowOn;
}
Time.fixedDeltaTime = 0.02 * Time.timeScale;
if(slowOn == true)
{
var pitchDown : float = gM.audio.pitch - (Time.deltaTime * pitchSpeedMultiplier);
gM.audio.pitch = pitchDown;
if(pitchDown <= slowPitch)
{
gM.audio.pitch = slowPitch;
}
var slowDown : float = Time.timeScale - (Time.deltaTime);
Time.timeScale = slowDown;
if (slowDown <= reducedTimeScale)
{
Time.timeScale = reducedTimeScale;
}
}
if(slowOn == false)
{
var pitchUp : float = gM.audio.pitch + (Time.deltaTime * pitchSpeedMultiplier);
gM.audio.pitch = pitchUp;
if(pitchUp >= normPitch)
{
gM.audio.pitch = normPitch;
}
var speedUp : float = Time.timeScale + (Time.deltaTime);
Time.timeScale = speedUp;
if (speedUp >= standardTimeScale)
{
Time.timeScale = standardTimeScale;
}
}
Here is some of the relevant code from my bullet shooting script:
//missile variables
var missileFireRate : float = 0.5;
private var missileNextShot : float = 0.0;
var missilePrefab : Transform;
var missileForce = Vector2(200,200);
...
if(Input.GetKey(fireWeapon) && SubControl.isShotEnabled == true)
{
if(primaryActive == true && missileActive == true)
{
if(Time.time >= missileNextShot) //Time.time is the time since frame has started
{
//Code generates Missile, and instantiates in the location of the Turret
var missileInstance = Instantiate(missilePrefab, Vector3(transform.position.x, GameObject.Find("Suber").transform.position.y, transform.position.z - 0.03), transform.rotation);
missileInstance.rigidbody2D.AddForce(Vector2.Scale(TurretRotation.difference, missileForce));
missileNextShot = Time.time + missileFireRate;
}
}
...
If anyone has any idea what's going on I would really appreciate the help!! :)
How are you moving your character? is it via an animation made in a package, or in script?
When timeScale is set to slow-mo/zero the game is basically slowed/paused if all your functions are frame rate INDEPEDENT.
Answer by Kiwasi · Sep 15, 2014 at 08:55 PM
Its recommended that you change the Time.fixedDeltaTime every time you change Time.timeScale.
I think I did that though (see line 6 of first block of code)
Your answer
Follow this Question
Related Questions
Slow and Fast motion? 1 Answer
How to get a smoother slow motion 0 Answers
Time.timeScale is laggy 2 Answers
Time.timeScale not working 1 Answer
Powerup Timer not Working 3 Answers