Log in or register to post comments

playing an animation after another animation

April 14, 2012 - 12:22pm #9

hi , I have two animations which I want to play one of them only after the completion of another . if it was a normal unity project I could easily use isPlaying , PlayQueued , or yield to control my animations , but in AR project I should only play my animations when the imageTarget is found . is there any way I can do this in c#? now that my animations are played together my OnTrackingFound function in DefaultTrackableEventHandler.cs looks like this .

public Animation myAnimation;
public Animation myAnimation2;

private void OnTrackingFound()
{
        myAnimation.Play();
        myAnimation2.Play();
		
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>      ();

        // Enable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = true;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + "       found");
		
}

Re: playing an animation after another animation

April 14, 2012 - 12:45pm #8

Take a look at the Unity Docs on Animation Events - http://unity3d.com/support/documentation/Components/animeditor-AnimationEvents.html.

Re: playing an animation after another animation

April 14, 2012 - 1:12pm #7

Thanks david , I've already considered using animation events but as someone else is doing the animations and it's in FBX format the animations are read-only and I can't insert events . I need a way to control my animations in the c sharp file

Re: playing an animation after another animation

April 14, 2012 - 3:50pm #6

Even if I use events in my animation , it seems it doesn't work , I used the below code for my event , it stops the animation but doesn't run it correctly after the waiting time . :(

var myAnim : Animation;

function myYield(){
	myAnim.Stop();
	yield WaitForSeconds(5);
	myAnim.Play();
}

Re: playing an animation after another animation

April 15, 2012 - 2:09am #5

I also tried it like this , though I see nothing wrong about it , it doesn't play myAnimation at all !

private void OnTrackingFound()
{
        myAnimation2.Play();
	StartCoroutine(WaitAndPlay(5.0f, myAnimation));

        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = true;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + "  found");
		
}


IEnumerator WaitAndPlay(float waitTime , Animation anim) {
        yield return new WaitForSeconds(waitTime);
        anim.Play();
}

Re: playing an animation after another animation

April 15, 2012 - 8:31am #4

Can you use PlayQueued if you are starting and stopping the Animation on the tracking events - rather than trying to synchronize the animations using a Coroutine.

Re: playing an animation after another animation

April 15, 2012 - 11:10am #3

thanks david , yes I could use PlayQueued , but one silly problem exists . I can't seems to access the name of my animation from DefaultTrackableEventHandler to use it in the function! you see in my code I've used myAnimation.Play() and myAnimation is a public variable which I set in my inspector view . I can't even play my animation like this : animation.Play("myclip"); !!! I don't know what's wrong but seems I just can't access the name of my animation :(

Re: playing an animation after another animation

April 15, 2012 - 12:08pm #2

here's the solution I found . I used an event in the first frame of my animation . here's the code of the function:

function myYield(){
animation["myAnim"].enabled = false;
yield WaitForSeconds(5);
animation["myAnim"].enabled = true;
}

thanks david

Re: playing an animation after another animation

April 17, 2012 - 5:08am #1
mojtaba_tabatabaie wrote:

here's the solution I found . I used an event in the first frame of my animation . here's the code of the function:

function myYield(){
animation["myAnim"].enabled = false;
yield WaitForSeconds(5);
animation["myAnim"].enabled = true;
}

thanks david

Hey mojtaba,
i guess, here u are not checking if the first animation is completed or not before you start the second animation.Rather you are giving a time delay for the second one.Can you please be a lil elaborate on this .Im also lookin for a solution for this.Thank you :)

Log in or register to post comments