Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Play animations after imagetarget detected

Discussion in 'Animation' started by lucnagel, Aug 16, 2018.

  1. lucnagel

    lucnagel

    Joined:
    Mar 15, 2018
    Posts:
    5
    Hi all, i'm currently working on an AR project which uses a combination of video's and animations. I editted the DefaultTrackableEventHandler.cs script so that the video starts playing when the image target is detected. I need to do the same for the animation clips, but haven't had any result so far. Is there something I'm missing? Any tips or advice would be appreciated!

    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. // need to import video functionality
    13. using UnityEngine.Video;
    14. [RequireComponent(typeof(VideoPlayer))]
    15.  
    16. /// <summary>
    17. ///     A custom handler that implements the ITrackableEventHandler interface.
    18. /// </summary>
    19. public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
    20. {
    21.     #region PRIVATE_MEMBER_VARIABLES
    22.  
    23.     protected TrackableBehaviour mTrackableBehaviour;
    24.     // setup the videoPlayer object
    25.     private VideoPlayer videoPlayer;
    26.     public Animation animationClips;
    27.  
    28.     #endregion // PRIVATE_MEMBER_VARIABLES
    29.  
    30.     #region UNTIY_MONOBEHAVIOUR_METHODS
    31.  
    32.     protected virtual void Start()
    33.     {
    34.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    35.         if (mTrackableBehaviour)
    36.             mTrackableBehaviour.RegisterTrackableEventHandler(this);
    37.  
    38.         // add the following 4 lines to get the reference to the video player component of the plane that the video is attached to
    39.         // IMPORTANT: set "Video_plane" to the name of the plane game object you attached your video to
    40.         GameObject video = GameObject.Find ("VideoPlane");
    41.         videoPlayer = video.GetComponent<VideoPlayer>();
    42.         animationClips = GetComponent<Animation>();
    43.         videoPlayer.Play();
    44.         videoPlayer.Pause();
    45.         // see the VideoPlayer Scripting API for more ideas on which functions you can use in your code
    46.         // for example changing the playback speed or jumping to a speicific point in time:
    47.         // https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html
    48.    
    49.     }
    50.  
    51.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    52.  
    53.     #region PUBLIC_METHODS
    54.  
    55.     /// <summary>
    56.     ///     Implementation of the ITrackableEventHandler function called when the
    57.     ///     tracking state changes.
    58.     /// </summary>
    59.     public void OnTrackableStateChanged(
    60.         TrackableBehaviour.Status previousStatus,
    61.         TrackableBehaviour.Status newStatus)
    62.     {
    63.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    64.             newStatus == TrackableBehaviour.Status.TRACKED ||
    65.             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    66.         {
    67.             //Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    68.             // Play the video:
    69.             animationClips.Play("Klok");
    70.             Debug.Log("Play!");
    71.             OnTrackingFound();
    72.             videoPlayer.Play();
    73.         }
    74.         else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
    75.             newStatus == TrackableBehaviour.Status.NOT_FOUND)
    76.         {
    77.             //Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    78.             // Pause the video, if using Stop() the video would always play back from the beginning again
    79.             animationClips.Stop("Klok");
    80.             Debug.Log("Stop!");
    81.             OnTrackingLost();
    82.             videoPlayer.Pause();
    83.         }
    84.         else
    85.         {
    86.             // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
    87.             // Vuforia is starting, but tracking has not been lost or found yet
    88.             // Call OnTrackingLost() to hide the augmentations
    89.             OnTrackingLost();
    90.         }
    91.     }
    92.  
    93.     #endregion // PUBLIC_METHODS
    94.  
    95.     #region PRIVATE_METHODS
    96.  
    97.     protected virtual void OnTrackingFound()
    98.     {
    99.         var rendererComponents = GetComponentsInChildren<Renderer>(true);
    100.         var colliderComponents = GetComponentsInChildren<Collider>(true);
    101.         var canvasComponents = GetComponentsInChildren<Canvas>(true);
    102.  
    103.         // Enable rendering:
    104.         foreach (var component in rendererComponents)
    105.             component.enabled = true;
    106.  
    107.         // Enable colliders:
    108.         foreach (var component in colliderComponents)
    109.             component.enabled = true;
    110.  
    111.         // Enable canvas':
    112.         foreach (var component in canvasComponents)
    113.             component.enabled = true;
    114.     }
    115.  
    116.  
    117.     protected virtual void OnTrackingLost()
    118.     {
    119.         var rendererComponents = GetComponentsInChildren<Renderer>(true);
    120.         var colliderComponents = GetComponentsInChildren<Collider>(true);
    121.         var canvasComponents = GetComponentsInChildren<Canvas>(true);
    122.  
    123.         // Disable rendering:
    124.         foreach (var component in rendererComponents)
    125.             component.enabled = false;
    126.  
    127.         // Disable colliders:
    128.         foreach (var component in colliderComponents)
    129.             component.enabled = false;
    130.  
    131.         // Disable canvas':
    132.         foreach (var component in canvasComponents)
    133.             component.enabled = false;
    134.     }
    135.  
    136.     #endregion // PRIVATE_METHODS
    137. }
    138.