Why is scripting animation not so responsive in unity?
Hi,
What am I doing:
Making a menu system!
What I Want:
I want to play an animation on my menu elements whenever the menu is in an open or closed state. However, my menu also has an open and close animation in their respective states. So when I transition from one menu to another I want the current menu elements animation to close first then its menu and when the next menu opens I want the menu to play open animation first then its elements. But this all depends on the kind of transition I am setting, therefore, I am coding it in a generic form, so it's fully configurable by the editor.
The Approach I Took:
I am using Unity's Mecanim system to achieve his.
I have attached animator to all the elements and menus that need animations on them.
I have made two scripts "UIManager.cs" and "ElementAnimator.cs".
I have attachment the "UIManager.cs" at the Canvas Level and "ElementAnimator.cs" at the Menu Level.
I have created multiple parameters for my menu animator for the various transitions.
In UI manager, I have created menu linkages and I also set the animator parameter when the transition OnClick event is called.
Problem with ElementAnimator.cs script:
This is the code I have written for ElementAnimator.cs:
public bool playMenuElements;
public bool value;
public Elements[] elements;
void SetBool(bool value) {
for (int i = 0; i < elements.Length; i++) {
if (elements [i].animator != null && elements [i].parameter != "")
elements [i].animator.SetBool (elements [i].parameter, value);
}
}
void Update() {
if (playMenuElements) {
Debug.Log (this.gameObject.name + " " + playMenuElements);
SetBool(value);
}
}
In the inspector, I key the playMenuElements variable to true or false at the desired frame. I set it back to false in the next frame in order to avoid any unnecessary memory usage. However, the problem is that the function SetBool() is called sometimes. Whys isn't unity working properly? Am I doing something wrong? Is my approach bad? Can anyone help me out in this? Can't think of anything, running out of options and ideas :(
What I Think
The call is dependent on the fps rate which explains the randomness in the SetBool call through the key. I have tried skipping few frames and then setting the variable to false. It gets called three or fours times, but it's still not consistent! I just need to call it once through the keyed variable.
Thanks in advance.
Your answer
Follow this Question
Related Questions
I can't make transition from actual animation to previous one 1 Answer
Unity C# Script error: CS1501 1 Answer
Animating in c# 1 Answer
Animations get screwed up when I add OnAnimatorMove function 0 Answers
MobController 1 Answer