- Home /
animation.speed does not work when disabling/enabling renderer
Hi,
The scenario I have is: I've imported an animation and I am pausing/playing using animation.speed (animation.speed = 1 - Plays, animation = 0 - Pauses). This part works fine. I also wanted to have the animation disappear while it is paused. When I disable the renderer for the components in the animated model the model does disappear correctly but animation continues to play and it not at the pause point when I reset the animation.speed back to 1 and enable the renderer for the compnents. I am setting animation.speed to 0 before cycling through all the children of the object and disabling the renderer for each component.
I wanted to find out why disabling the renderer is effecting the speed value for the animation or is it an issue on how I am instantiating the prefab
Notes
I have the animation/model defined as a public GameObject and it is set in the inspector
I've checked that I am getting the correct instance name and the correct renderer states and also
verified that the animation.speed value is getting set correctly on Pause to 0 and then back to 1The animation object is instantiated when the user presses a button to play the animation (routine called from OnGUI()) and then the animation starts playing.
The Pause/Play work fine as long as I do not disable the renderer for the object components.
The Pause/Play code are separate routines called from inside the Update() function.
Has anyone run into this issue or have any suggestions. Thanks! (Please note, this is the first time I've posted in Unity Answers)
The basic code snippets are below.
public GameObject Model; //Prefab set in the Inspector
..
..
Code to instantiate the object:
AnimationInstance = (GameObject)Instantiate(Model,transform.position, transform.rotation)
....
AnimationInstance.animation.Play();
Code for Pausing the animation and disabling the renderer
AnimationInstance.animation["Take 001"].speed = 0.0f;
Renderer[] rendererComponentsOff = AnimationInstance.GetComponentsInChildren<Renderer>();
// Disable rendering:
foreach (Renderer componentOff in rendererComponentsOff) {
//Debug.Log("COMPONENT NAME for renderOFF = false: " + componentOff.name);
componentOff.enabled = false;
}
Code for starting the animation back up from the pause point
AnimationInstance.animation["Take 001"].speed = 1.0f;
Renderer[] rendererComponentsOn = AnimationInstance.GetComponentsInChildren<Renderer>();
// Disable rendering:
foreach (Renderer componentOn in rendererComponentsOn) {
//Debug.Log("COMPONENT NAME for renderON = true " + componentOn.name);
componentOn.enabled = true;
}
Answer by PAEvenson · Jan 09, 2013 at 10:01 PM
Your instantiate is fine. I would suggest recording the animations time when you pause the animation and set the time when you replay the animation after a pause. To get the time its:
AnimationInstance.animation["Take 001"].time
Thanks for the info. I tried using pauseTime = AnimationInstance.animation["Take 001"].time to get the pause time but I get the same result when I try to resume the animation after disabling/enabling the renderer for the components. If I comment out the disable/enable renderer code then using AnimationInstance.animation["Take 001"].time works fine and the animation will resume playing from the paused time (pauseTime)
There's is something about disabling the renderer that is effecting it. I thought if you disable the renderer you still have the ability to work on the object (as opposed to making the object inactive - ex. AnimationInstance.active = false.)
did you try turning the renderers on BEFORE you set the time or resume the animation?
Yes, I've tried that, the way I have it is:
On Pause: get the current time of the animation turn off renderers
On resume: Turn on renderes play animation from pauseTime
Youre not calling Play() are you? This will restart it. All you want to do when you resume is set the speed and the time.
No, I'm not calling Play(). Below is the new code for resu$$anonymous$$g the animation. It's as if the animation continues to play in the background after I've set the speed to 0 to pause. When I re-enable the renderer, set speed back to 1 and resume at the specific pauseTime the animation is at whatever point it would be in the timeline as if the pause never happened.
Renderer[] rendererComponentsOn = AnimationInstance.GetComponentsInChildren();
// Disable rendering: foreach (Renderer componentOn in rendererComponentsOn) { componentOn.enabled = true; } AnimationInstance.animation["Take 001"].speed = 1.0f; AnimationInstance.animation["Take 001"].time = pausedTime;