- Home /
Mecanim Initial State Transition 1 Frame late
Hello everybody,
I got a little issue I'm running into regularly: I got an object with an animator attached which has an initial state where it is practically instantly made invisible. But in reality it is not turned invisible instantly but flashes up for a frame.
The only workaround so far is to have the object made manually invisible in the inspector while editing which is really tedious if I want to change something. (Because I have to make it visible again, make my edits and turn it back invisble again, when I'm done)
My biggest problem with this is, that the animator seems to need a whole frame until it finally applies the first frame of it's initial state.
Any thoughts on that. Anyoune experienced the same issue?
Can you add in a frame 0? Never tried, it may not work.
I don't think that's even possible! I assume if the first frame already has an index it will be 0.
Answer by tBurger · Oct 14, 2015 at 09:51 AM
Okay I found a way!
Another lead to this problem was the though the animator gets updated to late. So I've added this:
void OnEnable()
{
_animator.Update(Time.deltaTime);
}
Which forces the animator to update (i.e. to do whatever is set in the first frame of it's initial state) ahead of it's usual time. Seems to work so far!
It works even with a null or negative deltaTime value. It seems that the Unity animator just needs any kind of Update to kick off and update the animated properties immediately. Therefore, if you want to start your animation from the beginning, you can use animator.Update(0f)
If you want to skip the 1st frame, pass the animation frame period, etc.
Answer by anilo · Oct 13, 2015 at 09:58 PM
You should add the make it invisible logic to your awake function.
Unfortunately if I want to set a property in script that is already controlled by an animation curve the script changes are ignored.
Answer by huulong · Oct 27, 2016 at 03:10 PM
If your animator's Update Mode is set to Animate Physics, your object will refresh at a different rate than the view which may cause the flash. If you don't need physics update, set the Update Mode to Normal.
I tried on my project and it worked, but I still wanted to have physics update so instead I decided to initialize the object on Awake in a initial invisible state, and to switch to a visible state later in the game when the object is used.
If you cannot do that either, you can do like anilo said, making the target object invisible in your script, just after the object is created (so either Awake or in the method that spawn the object). If the rendered object is at the component root, you cannot just deactivate the object since it will also prevent the animator from updating, but you can deactivate the rendering components.
In your comment, you said that the animation curve would override your changes. But since your initial state is also supposed to make the object invisible (hence the flash), it should not revert what you did in the script so the object will start and remain invisible until the animation makes it visible.
Your answer
Follow this Question
Related Questions
Mecanim control animation frames via script? 2 Answers
Mecanim How Animate Bone a Ball Using Humanoid? 0 Answers
Is there a way to use physics to keep a character from clipping through itself during an animation? 0 Answers
Mecanim Anim screwing up character 2 Answers
Dynamically attaching a skinnedMeshRenderer to existing character 0 Answers