Unity AR Vuforia-如果触发则开始动画

时间:2018-10-04 12:52:12

标签: c# unity3d animation augmented-reality vuforia

Hiho社区,

我在我的AR-Android-App中使用Vuforia,并且我有一个带有动画的对象,我尝试仅当触发该对象时才播放动画。没有错误,但是没有用。这是我的代码:

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");
                }
            }
    }

和一些项目屏幕截图:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试将脚本附加到imageTarget,然后获取子对象的动画设计器组件(将其从场景或层次结构面板拖放到检查器中的脚本变量中)。

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");
    }
}

免责声明:代码未经测试。