Topic locked

Problems with tracking lost

September 29, 2014 - 2:58am #1

Hi,

 

I am having trouble getting my code to detect when tracking is lost, it just does not seem to fire?

 

I have a GameObject under a plane under my TargetImage and when tracking is found the GameObject is moved up through the TargetImage and works great.

But, when the tracking is lost I need to reset the GameObject back to its starting position, but as you can see from the code snippet although I have Debug.log calls in the state change, lost and found methods I only get state changed when found and when I remove my TargetImage from view nothing else fires?

???

 

public class TrackableEventHandler : MonoBehaviour,
                                     ITrackableEventHandler
{


	public GameObject MyObject;

    #region PRIVATE_MEMBER_VARIABLES

    private TrackableBehaviour mTrackableBehaviour;

    private bool mHasBeenFound = false;
    private bool mLostTracking;
    //private float mSecondsSinceLost;

    #endregion // PRIVATE_MEMBER_VARIABLES



    #region UNITY_MONOBEHAVIOUR_METHODS

    void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }

        OnTrackingLost();
    }


    void Update()
    {
    }

    #endregion // UNITY_MONOBEHAVIOUR_METHODS
	
    #region PUBLIC_METHODS

    /// <summary>
    /// Implementation of the ITrackableEventHandler function called when the
    /// tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {

		Debug.Log ("state change");

        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS



    #region PRIVATE_METHODS

    private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
        Collider[] colliderComponents = GetComponentsInChildren<Collider>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = true;
        }

        // Enable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = true;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");

        mHasBeenFound = true;
        mLostTracking = false;
    }


    private void OnTrackingLost()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
        Collider[] colliderComponents = GetComponentsInChildren<Collider>();

        // Disable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = false;
        }

        // Disable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = false;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");

        mLostTracking = true;
		mHasBeenFound = false;
 
    }


    // Pause all videos except this one
    private void PauseOtherVideos(VideoPlaybackBehaviour currentVideo)
    {
        VideoPlaybackBehaviour[] videos = (VideoPlaybackBehaviour[])
                FindObjectsOfType(typeof(VideoPlaybackBehaviour));

        foreach (VideoPlaybackBehaviour video in videos)
        {
            if (video != currentVideo)
            {
                if (video.CurrentState == VideoPlayerHelper.MediaState.PLAYING)
                {
                    video.VideoPlayer.Pause();
                }
            }
        }
    }

    #endregion // PRIVATE_METHODS
}

 

Problems with tracking lost

December 15, 2014 - 4:15am #4

-closing thread.

Problems with tracking lost

December 12, 2014 - 12:44pm #3

It seems that I have not been seeing my threads getting answered and so looks like I have been ignore response!

 

Apologies this is my bad!

 

Thanks, I have since resolved this by following the samples and replicating

Problems with tracking lost

October 2, 2014 - 4:00pm #2

A few troubleshooting steps to try:

1. Compare your code to the ImageTarget sample app to see what's different.
2. Add Debug.Log() commands to beginning of methods to monitor call flow.
3. Set breakpoints in MonoDevelop and use debugger.

Topic locked