Created
November 10, 2017 11:44
-
-
Save omarojo/fb09fda4cf04449670ec08c5912f6411 to your computer and use it in GitHub Desktop.
Unity Trigger Animation Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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