Reputation: 165
Hiho Community,
I use Vuforia for my AR-Android-App and I have a object with an animation and I try that it only plays the animation if the object is triggered. No errors but it doesn´t work. Here my code:
using UnityEngine;
using Vuforia;
public class start_animation_if_trigger : MonoBehaviour, ITrackableEventHandler
{
private TrackableBehaviour mTrackableBehaviour;
Animator m_Animator;
void Start()
{
m_Animator = GetComponent<Animator>();
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
// Play audio when target is found
m_Animator.Play("C4D Animation Take");
}
else
{
// Stop audio when target is lost
m_Animator.Play("none");
}
}
}
and some project screenshots:
Upvotes: 1
Views: 2456
Reputation: 4166
Try to attach the script to the imageTarget and then get the animator component of the child object (drag and drop it from the scene or hierarchy panel into the script variable in the inspector).
public GameObject gameObjectToAnimate;
private Animator objectAnimator;
....
void Start () {
objectAnimator = gameObjectToAnimate.GetComponent<Animator>();
...
// 2s pause after instantiate the object
Invoke("StartAnimation", 2);
}
void StartAnimation() {
if (objectAnimator != null)
{
objectAnimator.PlayInFixedTime("C4D Animation Take");
}
}
Disclaimer: code not tested.
Upvotes: 1