Skip to content

Instantly share code, notes, and snippets.

@omarojo
Created November 10, 2017 11:44
Show Gist options
  • Save omarojo/fb09fda4cf04449670ec08c5912f6411 to your computer and use it in GitHub Desktop.
Save omarojo/fb09fda4cf04449670ec08c5912f6411 to your computer and use it in GitHub Desktop.
Unity Trigger Animation Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
namespace Vuforia{
public class ImageTargetAnimTrigger: MonoBehaviour,
ITrackableEventHandler
{
private TrackableBehaviour mTrackableBehaviour;
private GameObject myAudio;
private GameObject animationTimeline;
private bool audioStartedPlayingAlready = false;
void Start()
{
myAudio = GameObject.Find("AudioBG");
animationTimeline = GameObject.Find("Timeline");
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
print ("CHANGED STATE");
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
// Play audio when target is found
if(audioStartedPlayingAlready == false){
myAudio.GetComponent<AudioSource>().Play();
audioStartedPlayingAlready = true;
}
animationTimeline.GetComponent<PlayableDirector>().Play();
}
else
{
// Stop audio when target is lost
// myAudio.GetComponent<AudioSource>().Pause();
animationTimeline.GetComponent<PlayableDirector>().Pause();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment