Log in or register to post comments

Replay animation after target is lost

September 12, 2019 - 11:23am #1

Hey everyone!

So i made a simple animation for my AR project and works fine but when the target is lost and detected again, it show the animation in its last state: idle.

So, i wanna know if there is a way to restart the animation when the target is detected again. I tried to use the Animator API on the handler script but i am too newbie to coding so, i cant make it work.

Any help would be great! Thanks!

Replay animation after target is lost

September 13, 2019 - 5:28am #3

Hi,

 

I checked the scene that you mentioned and i tried to implement the code into my scene but nothing happens.

 

This is my modified code for the handler:

 

public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler

{

    public Animator cinco;

    #region PROTECTED_MEMBER_VARIABLES

    protected TrackableBehaviour mTrackableBehaviour;

    protected TrackableBehaviour.Status m_PreviousStatus;

    protected TrackableBehaviour.Status m_NewStatus;

    #endregion // PROTECTED_MEMBER_VARIABLES

    #region UNITY_MONOBEHAVIOUR_METHODS

    protected virtual void Start()

    {

       

        mTrackableBehaviour = GetComponent<TrackableBehaviour>();

        if (mTrackableBehaviour)

            mTrackableBehaviour.RegisterTrackableEventHandler(this);

    }

    protected virtual void OnDestroy()

    {

        if (mTrackableBehaviour)

            mTrackableBehaviour.UnregisterTrackableEventHandler(this);

    }

    #endregion // UNITY_MONOBEHAVIOUR_METHODS

    #region PUBLIC_METHODS

    /// <summary>

    ///     Implementation of the ITrackableEventHandler function called when the

    ///     tracking state changes.

    /// </summary>

    public void OnTrackableStateChanged(

        TrackableBehaviour.Status previousStatus,

        TrackableBehaviour.Status newStatus)

    {

        m_PreviousStatus = previousStatus;

        m_NewStatus = newStatus;

       

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName +

                  " " + mTrackableBehaviour.CurrentStatus +

                  " -- " + mTrackableBehaviour.CurrentStatusInfo);

        if (newStatus == TrackableBehaviour.Status.DETECTED ||

            newStatus == TrackableBehaviour.Status.TRACKED ||

            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)

        {

            OnTrackingFound();

        }

        else if (previousStatus == TrackableBehaviour.Status.TRACKED &&

                 newStatus == TrackableBehaviour.Status.NO_POSE)

        {

            OnTrackingLost();

        }

        else

        {

            // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND

            // Vuforia is starting, but tracking has not been lost or found yet

            // Call OnTrackingLost() to hide the augmentations

            OnTrackingLost();

        }

    }

    #endregion // PUBLIC_METHODS

    #region PROTECTED_METHODS

    protected virtual void OnTrackingFound()

    {

        cinco.SetBool("cincoAnimado", true);

        if (mTrackableBehaviour)

        {

            var rendererComponents = mTrackableBehaviour.GetComponentsInChildren<Renderer>(true);

            var colliderComponents = mTrackableBehaviour.GetComponentsInChildren<Collider>(true);

            var canvasComponents = mTrackableBehaviour.GetComponentsInChildren<Canvas>(true);

            // Enable rendering:

            foreach (var component in rendererComponents)

                component.enabled = true;

            // Enable colliders:

            foreach (var component in colliderComponents)

                component.enabled = true;

            // Enable canvas':

            foreach (var component in canvasComponents)

                component.enabled = true;

        }

     

    }



    protected virtual void OnTrackingLost()

    {

        cinco.SetBool("cincoAnimado", false);



        if (mTrackableBehaviour)

        {

            var rendererComponents = mTrackableBehaviour.GetComponentsInChildren<Renderer>(true);

            var colliderComponents = mTrackableBehaviour.GetComponentsInChildren<Collider>(true);

            var canvasComponents = mTrackableBehaviour.GetComponentsInChildren<Canvas>(true);

            // Disable rendering:

            foreach (var component in rendererComponents)

                component.enabled = false;

            // Disable colliders:

            foreach (var component in colliderComponents)

                component.enabled = false;

            // Disable canvas':

            foreach (var component in canvasComponents)

                component.enabled = false;

        }

    }

    #endregion // PROTECTED_METHODS

}

 

_______________________________________________________________

 

Its like a need to create some kind of parameter for cincoAnimado but i am not sure because i dont understand whats going on.

 

Forgot to mention that in my case, i have two objects for the same target with two different animators. And i have to set Culling to Completely for the animation to do something and i notice that in your scene, is set to Always so, another thing that i dont understand u.u

 

Thanks!

 

Replay animation after target is lost

September 13, 2019 - 1:01am #2

Hi,

In the Vuforia Core Sample app, in the scene: 3-MultiTargets we demo exactly what you want to achieve. The animation stops when tracking is lost and starts when tracking is found again.

Script: MultiTargetTrackableEventHandler.cs handles part of the logic.

Thank you.

Vuforia Engine Support

Log in or register to post comments