- Home /
Time timeScale and Animations
Hello, I'm trying to create a simple pause menu, while the game is paused I set the Time.timeScale to zero, which pauses everything wonderfully. However I noticed that a few of my button animations are running incorrectly?
// Pause Controller
if (Input.GetKeyDown(KeyCode.Escape))
{
// Paused, or unpaused
pauseMenu.GetComponent<PauseMenuScript>().Display = !pauseMenu.GetComponent<PauseMenuScript>().Display;
pauseMenu.SetActiveRecursively(pauseMenu.GetComponent<PauseMenuScript>().Display);
if (pauseMenu.GetComponent<PauseMenuScript>().Display)
{
// Paused
Time.timeScale = 0;
}
else
{
// Remove menu, and re-enable the players, laptimer, etc....
Time.timeScale = 1;
}
}
// End Pause Controller
That is the logic that pauses my game.
The script that handles the button animations looks like this:
using UnityEngine;
using System.Collections;
public class btnAnim : MonoBehaviour
{
public AnimationClip onAnim;
void OnMouseEnter() {
animation.AddClip(onAnim, "on");
animation["on"].speed = 1.0F;
animation.Play("on");
}
void OnMouseExit() {
animation["on"].speed = -1.0F;
animation["on"].time = animation["on"].length;
animation.Play("on");
}
void OnMouseUp() {
gameObject.SendMessage("onClick", SendMessageOptions.DontRequireReceiver);
}
}
This script takes the button it is attached to and makes it slowly appear brighter when it is moused over due to the animation I'm using. It works fine for all the other buttons through out the game perfectly until I display the pause menu when the Time.timeScale = 0. If I drag my pause menu prefab into a empty scene it works fine. So I was wondering if it was the Time.timeScale = 0 that's making my animation look like its running backwards or if it was something else entirely.
What I mean by backwards is on mouse over the first time nothing happens, and then when you leave the button it animates the mouse over animation and when you go back it animates the on mouse leave animation.
Thanks in advance, Hans
Answer by Anxo · Jun 07, 2011 at 07:21 PM
Your buttons are animated on frame basis. Updates and stuff is not effected by Time. It will continue to run your code. To solve this you can add Time to your button animations. So instead of moving it by 1, you can move it by 10*Time.deltaTime or so. That will make your button animation move relative to your game time.
Thanks that fixed my animation problems :D greatly appreciated.
Answer by Orrib · Jan 08, 2016 at 10:45 AM
The easiest solution is to go under the animator on your gameobject, then go under "Update Mode" and choose "Unscaled Time."This way you the animator isn't affected by timescale modification. This method saves a lot of time and effort, and is coding-free!
Answer by Fr0sZ · Jun 07, 2011 at 07:31 PM
You can also have a bool variable that checks if it pasued.
if(!paused) //if not paused
{
//do stuff
}
Your answer
Follow this Question
Related Questions
Single Step (pause, resume), or externally clock game loop 0 Answers
Time.timeScale not working with Havok Physics 0 Answers
Problem changing TimeScale 1 Answer
'Time' does not contain a definition for 'timeScale' 1 Answer
Slow Time in Area 0 Answers