- Home /
Can I make an animation while game is paused?
Hi, I'm using unity 2d and I have a pause function which just makes the time scale switch to 0 (and then to 1, when activated again). Now the whole game is stopped even the animations. What I wanna do is just have an animation when it is paused but obviously it doesn't work because it's paused. Is there a way where I can do that, maybe by just letting that animation contradict? Can someone please gimme any suggestions?
here is the code I have if anyone wants to see c##
void Update () {
if(Input.GetKeyUp(KeyCode.P))
{
paused = !paused;
}
if (paused) {
Time.timeScale = 0;
anim.SetTrigger ("paused");
} else {
Time.timeScale = 1;
showControls = false;
}
}
Answer by Maui-M · Feb 21, 2014 at 10:20 PM
Instead of using Time.timescale just run different parts of your code depending on if your game is paused or not.
void Update () {
if(Input.GetKeyUp(KeyCode.P)) {
paused = !paused;
}
if (paused) {
// Run pause code here
anim.SetTrigger ("paused");
} else {
// Run game code here
showControls = false;
}
}
Yup, you will be happier if you go with making your own pause logic and not messing with the timescale. I like to make either a static class or singleton to manage pausing and other game states.
Well a singleton will be simpler. $$anonymous$$ake a custom class with a static variable of the class and then instantiate the class in the static variable. This way you have the class inside of it's self and can access all of the class from anywhere.
//manager classs with singleton
public class Game$$anonymous$$anager
{
public static Game$$anonymous$$anager instance = new Game$$anonymous$$anager();
public bool paused = false;
public void TogglePause()
{
paused = !paused;
}
public void PauseGame()
{
paused = true;
}
public void UnpauseGame()
{
paused = false;
}
}
//other script you are using
public class $$anonymous$$yGameScript : $$anonymous$$onobehaviour
{
//stuff
void Update()
{
//This will pause and unpause the game.
if(Input.Gey$$anonymous$$ey("P"))
Game$$anonymous$$anager.instance.TogglePause();
if(Game$$anonymous$$anager.instance.paused == false)
{
//logic you want to run when paused.
}
else
{
//funcatlity to run when the game is paused.
}
}
//more stuff
}
Answer by unigeek · Dec 28, 2014 at 10:55 AM
If you use Animator to control your animations you may set it to use unscaled time for its update mode. The same way, within the Update() method you may use Time.unscaledDeltaTime for your "pause independent" logic in place where you would normally use Time.deltaTime (e.g. you want to be able to move camera around while your game paused). I tested this with Unity 4.6 - works well and saves you from writing tons of custom stuff.
We mostly go by votes here. It doesn't matter if a Q has an accepted answer, or which one it is. The person who asked the Q can "Accept" the one that helped them. That's good for 1-in-1. But Q's here are meant to help everyone - the OP's best answer may not be the one the helps everyone else. Also, this particular response was posted 11 months after the Q, so the asker was probably long gone by then.
Answer by Owen-Reynolds · Feb 21, 2014 at 11:11 PM
If you have to set timescale to 0, you can hand-drive an animation by manually changing its time
variable:
void Update() {
animation["bob"].time += 0.01f;
It won't be frame-rate independent (since you can't use Time.deltaTime,) but trial&error can probably get it close enough.
if it is a image sprite then. I mean after my player destroyed the whole game is paused.(am using Time.timeScale=0) i need to show a canvas image sprite .
it contains save me option.
how can i do this
thanks for reading