- Home /
How to pause only specific objects/prefabs?
I would like to be able to pause my game via script, but leave specific prefabs (in this case my camera) not paused and moving as per normal. Here is the scenario:
When my character gets a certain distance from an enemy I would like to be able to pause my game (for a specified time) BUT not pause my camera so that it can turn towards the enemy while everything else is paused. I've been looking at Time.timescale and using it for the "pause" functionality but I don't know how to pause everything except the camera. Would something like this be possible?
Answer by Berenger · Jan 29, 2013 at 12:27 PM
You could use a custom Update function on everything that need an Update and can be paused, something like :
public abstract class MonoPause : MonoBehaviour // Any resemblance to lady stuff is purely coincidental ...
{
public bool pause = false;
private IEnumerator Start()
{
while( Application.IsPlaying )
{
if( !pause )
DoUpdate();
yield return null;
// You could even have a time variable here
}
}
// That's where it happens !
protected abstract void DoUpdate();
}
and ...
public class Pouet : MonoPause
{
protected override void DoUpdate()
{
// Movement and stuff
}
}
So there's no way to apply a time.timescale to only specific prefabs for example?
Not right away, Unity's timeScale will affect everything using time, or deltaTime. You'll have to go through something similar to the code above to have a more detailed time.
Nop, Unity's timeScale affect everything using time or deltaTime. You'll have to go through something similar to the code above for a finer handling of time.
By the way, you don't interact with prefabs ingame, only instances of them ;)
I found a similar question here:
http://answers.unity3d.com/questions/222499/stop-time-for-specific-objects.html
It uses a timescale very close to zero and then multiplies the needed component variables by the inverse (see the link above for more details). In my case I think this is a more straight forward approach since I only ever need one object "unpaused" during the pause. I was hoping that with the release of Unity 4 there would be a more elegant way to do this but I guess these 2 are still the only options at the moment.
Answer by wowza · Aug 20, 2014 at 06:18 PM
I think you should use time.fixedDeltaTime instead of time.deltaTime, that makes the prefab ignore the timescale==0. I'm only a beginner and this happened to me once, I hope I helped.
Answer by buckius82 · Nov 24, 2015 at 07:26 PM
hello i am writing an RTS i wanted to be able to pause only the scene not the camera.
i have a time manager script were i can set the time scale to zero which is perfect. however it paused the camera as well.
when you set time scale to 0 fixedupdate is never called on all monevelopements
all of my input for the camera was in the fixed update
all smoothing was done by using time.deltatime
to make the camera move independent of the time scale i changed the fixedupdate to lateupdate.
then instead of using time.deltatime for smoothing the input i used time.unscaledDeltaTime
using unscaled delta time also allows you to x2, x10 the scenes scale and keep the camera's movement seperate.