- Home /
Time.timeScale is laggy
Okay Im making kinda off like a SuperHot inspired game. The idea is that when you rightclick everything goes into slowmotion except the player. Ive done this by setting the Time.timeScale to 0.2f and then multiplying all charachter movement with 5 so that it has the same speed as if the Time.timeScale was set to 1 and it had its normal speeds. It works but its super laggy. While I still have 2000fps the character is seems to only be moving once every half second or so. Anyone have any explanation to this problem? heres my script: using UnityEngine; using System.Collections; public class AmazingSlowMo : MonoBehaviour { public float slowMotionSpeed = 5.0f; //public bool slowMoOn; //public bool slowMoOff; private MouseLook mouseLook; private MouseLook mouseLook2; private CharacterMotor charMotor; void Awake () { mouseLook = transform.GetComponent<MouseLook>(); mouseLook2 = GameObject.FindGameObjectWithTag(Tags.mainCamera).GetComponent<MouseLook>(); charMotor = transform.GetComponent<CharacterMotor>(); } void Update() { if (Input.GetButtonDown("Fire2")) { if (Time.timeScale == 1.0F){ Time.timeScale = 0.2F; slowMotionSpeed = 5.0f; SlowMotionOn(); } else{ Time.timeScale = 1.0F; Time.fixedDeltaTime = 0.02F * Time.timeScale; slowMotionSpeed = 5.0f; SlowMotionOff(); } } } void SlowMotionOn(){ mouseLook.sensitivityX = mouseLook.sensitivityX * slowMotionSpeed; mouseLook2.sensitivityY = mouseLook2.sensitivityY * slowMotionSpeed; charMotor.movement.maxForwardSpeed = charMotor.movement.maxForwardSpeed * slowMotionSpeed; charMotor.movement.maxSidewaysSpeed = charMotor.movement.maxSidewaysSpeed * slowMotionSpeed; charMotor.movement.maxBackwardsSpeed = charMotor.movement.maxBackwardsSpeed * slowMotionSpeed; charMotor.movement.maxGroundAcceleration = charMotor.movement.maxGroundAcceleration * slowMotionSpeed; charMotor.movement.maxAirAcceleration = charMotor.movement.maxAirAcceleration * slowMotionSpeed; charMotor.movement.gravity = charMotor.movement.gravity * slowMotionSpeed; charMotor.movement.maxFallSpeed = charMotor.movement.maxFallSpeed * slowMotionSpeed; } void SlowMotionOff(){ mouseLook.sensitivityX = mouseLook.sensitivityX / slowMotionSpeed; mouseLook2.sensitivityY = mouseLook2.sensitivityY / slowMotionSpeed; charMotor.movement.maxForwardSpeed = charMotor.movement.maxForwardSpeed / slowMotionSpeed; charMotor.movement.maxSidewaysSpeed = charMotor.movement.maxSidewaysSpeed / slowMotionSpeed; charMotor.movement.maxBackwardsSpeed = charMotor.movement.maxBackwardsSpeed / slowMotionSpeed; charMotor.movement.maxGroundAcceleration = charMotor.movement.maxGroundAcceleration / slowMotionSpeed; charMotor.movement.maxAirAcceleration = charMotor.movement.maxAirAcceleration / slowMotionSpeed; charMotor.movement.gravity = charMotor.movement.gravity / slowMotionSpeed; charMotor.movement.maxFallSpeed = charMotor.movement.maxFallSpeed / slowMotionSpeed; } }
Answer by Kiwasi · Jul 11, 2014 at 04:12 AM
Your CharacterMotor class is probably doing movement in FixedUpdate(). To change the speed of FixedUpdate you need to change Time.fixedDeltaTime every time you change Time.timeScale. In other words copy line 25 to line 19.
That works great thank you. One more question though. I still seem to be sliding around when in slowmotion. Even if i disable sliding or put all values to 0. Would you happen to know why that is?
Answer by Pysassin · Jul 11, 2014 at 04:12 AM
Have you tried using a unscaled time for using character movement in your motor? I mean the actual script that translates or adds force? I'm referring to http://docs.unity3d.com/ScriptReference/Time-unscaledDeltaTime.html. Using that means even if you change the Time.timeScale you don't need to alter anything on your character because it will run just like it did before.
That's a neat trick.
Just be warned it could cause problems if your game ever requires you to adjust the player speed.