- Home /
How do you move a player controlled object when the Time.timescale is zero?
I have a basic script that pause everything with time.timescale and I have another script that should allow player movement when the screen is paused but it doesnt work. What am I doing wrong that makes this not controllable?
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
moveVelocity = moveInput * speed;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + moveVelocity * Time.unscaledDeltaTime);
}
The documentation says it all:
https://docs.unity3d.com/ScriptReference/Time-timeScale.html
FixedUpdate functions will not be called when timeScale is set to zero.
So if FixedUpdate doesnt work, what do I need to make it work ins$$anonymous$$d?
If you need collision detection when Time.timeScale
is 0, you will have to handle collision detection by yourself.
Otherwise, you don't set the timeScale
to 0 and you "stop" all the objects/animations "by hand".
There may be other ways.
Answer by Cornelis-de-Jager · Jul 17, 2019 at 05:24 AM
Replace rb.MovePosition(rb.position + moveVelocity * Time.unscaledDeltaTime
with rb.MovePosition(rb.position + moveVelocity * 0.001f
Let me explain, you replace all references to the time values by constants small enough to represent would that time would have been if normalt. In this case 0.001f
. You can pop this into a variable and experiment with it.