1
\$\begingroup\$

I have an object animation which plays on the opening of a scene. When the animation is over I want the UI slider to be setActive - allowing the user to rotate the object. I have both these elements in place and working individually - just can't quite work out how to go about combining them.

Any help or advice about what approach I should take is much appreciated.

The animation was created in Maya.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You can do it by waiting until the animation ends. You can use IEnumarator to do it if your function is not in the Update loop.

public Animator anim;
public Slider slider;


public void WaitAnimaitonEnds()
{
    StartCoroutine(WaitTillAnimEnds());
}

IEnumerator WaitTillAnimEnds()
{

    yield return new WaitForSeconds((anim.GetCurrentAnimatorStateInfo(0).length + anim.GetCurrentAnimatorStateInfo(0).normalizedTime));
    // do whatever you want
    slider.SetActive(true);
    // do whatever you want
}

If you are looking for the alternative ways to do it, you can check the documentation here.

\$\endgroup\$
1
\$\begingroup\$

From Unity Answers:

I find it easier to just add AnimationEvents to the clip at the start and/or end which, in your case, stop and start the movement by calling those functions. It's much more reliable than using a CoRoutine because an AnimationState might get interrupted and this wouldn't be caught be the CR timer.

From StackOverflow:

You can use events on animation clips. It's explained in Unity manual: https://docs.unity3d.com/Manual/AnimationEventsOnImportedClips.html

In Animation Inport Settings in Annimations tab You can find Event heading. Position the playback to the end and click Add Event. Fill the Function field with name of the function to call at the end of animation. Just make sure that Game Object with this animation has a corresponding function.


Another approach, from Unity Answers:

This is super old thread, but as far as i can see, "Add Behaviour" in Animator can solve this problem. That will create the script for your state, which has several methods handy for such requirements.

Check this out: https://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html

Cheers

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .