Search Unity

Animation restart when Target is found again.

Discussion in 'Vuforia' started by khaliqmk, Mar 1, 2018.

  1. khaliqmk

    khaliqmk

    Joined:
    Mar 1, 2018
    Posts:
    1
    Hi,
    I've been trying to change the coding in Default trackable event handling (script) to start the animation again as the target image is found again. and stop the animation when target is lost.

    This is the coding that is default. The code mentioned in bold is what I changed the audio in the coding to restart the audio every time the target is lost and found again. Now I want the same thing for animation. It would be great if someone can help me out with this.

    /*==============================================================================
    Copyright (c) 2017 PTC Inc. All Rights Reserved.

    Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
    All Rights Reserved.
    Confidential and Proprietary - Protected under copyright and other laws.
    ==============================================================================*/

    using UnityEngine;
    using Vuforia;

    /// <summary>
    /// A custom handler that implements the ITrackableEventHandler interface.
    /// </summary>
    public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
    {
    #region PRIVATE_MEMBER_VARIABLES

    protected TrackableBehaviour mTrackableBehaviour;

    #endregion // PRIVATE_MEMBER_VARIABLES

    #region UNTIY_MONOBEHAVIOUR_METHODS

    protected virtual void Start()
    {
    mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    if (mTrackableBehaviour)
    mTrackableBehaviour.RegisterTrackableEventHandler(this);
    }

    #endregion // UNTIY_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)
    {
    if (newStatus == TrackableBehaviour.Status.DETECTED ||
    newStatus == TrackableBehaviour.Status.TRACKED ||
    newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    {

    AudioSource audio = GetComponent<AudioSource> ();
    audio.Play ();
    }
    else
    {
    AudioSource audio = GetComponent<AudioSource> ();
    audio.Stop ();

    }
    }

    #endregion // PUBLIC_METHODS

    #region PRIVATE_METHODS

    protected virtual void OnTrackingFound()
    {
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = 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()
    {
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = 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 // PRIVATE_METHODS
    }
     
  2. Vuforia-Strasza

    Vuforia-Strasza

    Official Vuforia Employee Vuforia

    Joined:
    Jun 13, 2017
    Posts:
    548
    What have you tried so far in attempting to get this work? Are you encountering a specific issue?
     
  3. theolagendijk

    theolagendijk

    Joined:
    Nov 12, 2014
    Posts:
    109
    Hi khaliqmk,

    Would be great if you put your code into a c# code block. Makes it way more readable.

    Code (CSharp):
    1. /*==============================================================================
    2. Copyright (c) 2017 PTC Inc. All Rights Reserved.
    3.  
    4. Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
    5. All Rights Reserved.
    6. Confidential and Proprietary - Protected under copyright and other laws.
    7. ==============================================================================*/
    8.  
    9. using UnityEngine;
    10. using Vuforia;
    11.  
    12. /// <summary>
    13. ///     A custom handler that implements the ITrackableEventHandler interface.
    14. /// </summary>
    15. public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
    16. {
    17.     #region PRIVATE_MEMBER_VARIABLES
    18.  
    19.     protected TrackableBehaviour mTrackableBehaviour;
    20.  
    21.     #endregion // PRIVATE_MEMBER_VARIABLES
    22.  
    23.     #region UNTIY_MONOBEHAVIOUR_METHODS
    24.  
    25.     protected virtual void Start()
    26.     {
    27.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    28.         if (mTrackableBehaviour)
    29.             mTrackableBehaviour.RegisterTrackableEventHandler(this);
    30.     }
    31.  
    32.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    33.  
    34.     #region PUBLIC_METHODS
    35.  
    36.     /// <summary>
    37.     ///     Implementation of the ITrackableEventHandler function called when the
    38.     ///     tracking state changes.
    39.     /// </summary>
    40.     public void OnTrackableStateChanged(
    41.         TrackableBehaviour.Status previousStatus,
    42.         TrackableBehaviour.Status newStatus)
    43.     {
    44.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    45.             newStatus == TrackableBehaviour.Status.TRACKED ||
    46.             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    47.         {
    48.          
    49. AudioSource audio = GetComponent<AudioSource> ();
    50.             audio.Play ();
    51.         }
    52.         else
    53.         {
    54.             AudioSource audio = GetComponent<AudioSource> ();
    55.             audio.Stop ();
    56.         }
    57.     }
    58.  
    59.     #endregion // PUBLIC_METHODS
    60.  
    61.     #region PRIVATE_METHODS
    62.  
    63.     protected virtual void OnTrackingFound()
    64.     {
    65.         var rendererComponents = GetComponentsInChildren<Renderer>(true);
    66.         var colliderComponents = GetComponentsInChildren<Collider>(true);
    67.         var canvasComponents = GetComponentsInChildren<Canvas>(true);
    68.  
    69.         // Enable rendering:
    70.         foreach (var component in rendererComponents)
    71.             component.enabled = true;
    72.  
    73.         // Enable colliders:
    74.         foreach (var component in colliderComponents)
    75.             component.enabled = true;
    76.  
    77.         // Enable canvas':
    78.         foreach (var component in canvasComponents)
    79.             component.enabled = true;
    80.     }
    81.  
    82.  
    83.     protected virtual void OnTrackingLost()
    84.     {
    85.         var rendererComponents = GetComponentsInChildren<Renderer>(true);
    86.         var colliderComponents = GetComponentsInChildren<Collider>(true);
    87.         var canvasComponents = GetComponentsInChildren<Canvas>(true);
    88.  
    89.         // Disable rendering:
    90.         foreach (var component in rendererComponents)
    91.             component.enabled = false;
    92.  
    93.         // Disable colliders:
    94.         foreach (var component in colliderComponents)
    95.             component.enabled = false;
    96.  
    97.         // Disable canvas':
    98.         foreach (var component in canvasComponents)
    99.             component.enabled = false;
    100.     }
    101.  
    102.     #endregion // PRIVATE_METHODS
    103. }
    And with regards to your question, there's a C# scripting API for animations ; https://docs.unity3d.com/ScriptReference/Animation.html

    Cheers.
     
    Vuforia-Strasza likes this.
  4. khanrpg

    khanrpg

    Joined:
    Apr 21, 2018
    Posts:
    1
    Good afternoon friends, I also have a doubt similar to this one. How do I make the animation start only when I start tracking? How do I edit scritp Default trackable event handling?

    I tried the following:
    1) Creating the variable:

    public class DefaultTrackableEventHandler: MonoBehaviour, ITrackableEventHandler
    {
    public UnityEngine.Animator animacao;
    #region PROTECTED_MEMBER_VARIABLES

    protected TrackableBehaviour mTrackableBehaviour;


    2) Calling:

    protected virtual void OnTrackingFound ()
    {
    animacao.Play;


    But it did not work.

    Any ideas?
    A hug :)
     
  5. augusty84

    augusty84

    Joined:
    Sep 6, 2014
    Posts:
    1
    Hi Everyone, It seems like such a simple idea and an obvious one? Yet Vuforia does not provide a solution. Sometimes we need to think differently to find the right solution. I did find this out and it works for me: Instantiate the game object when detected will then start the animation.
    https://library.vuforia.com/articles/Solution/Working-with-Vuforia-and-Unity.html

    Follow the Instructions For:
    How To Dynamically Add Content to Targets in Unity

    Hey Everyone if you find a solution to a problem - please put it pack in your post.
     
    Last edited: Dec 4, 2018
  6. cretinous

    cretinous

    Joined:
    Feb 24, 2019
    Posts:
    4
    Have you ever got an answer for this??
     
  7. rafazoedea

    rafazoedea

    Joined:
    Jun 12, 2019
    Posts:
    1
    PODEMOS OBTER AJUDA?
    PELO QUE OBSERVO É UMA SIMPLES QUESTÃO, POREM NÃO ENCONTRAMOS RESPOSTA?