Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Help with Vuforia project

Discussion in 'Editor & General Support' started by Spinnernicholas, Dec 4, 2013.

  1. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Moved from answers: http://answers.unity3d.com/questions/585960/

    I created this thread for tredpro.

    He needs a lot of help with his project, more than Answers can support, so I am moving the discussion here.

    So, First off, tredpro, please describe your project.

    What are your outcomes? That is, what is your project supposed to be when it is finished? What is the purpose of the finished project? How/Why does your audience use it?

    I'll update this post as we get more information.
     
    Last edited: Dec 4, 2013
  2. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    You are suppose to put a card down and 2 things appear using vuforia. 1st is the monster in it's idle position and 2nd is an attack button. you press the attack button an the monster attacks and then goes back to idle position.
     
  3. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Ok, that is kind of what I imagined. Where is your project at so far? Is any of it working?
     
  4. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    all but getting it to attack
     
  5. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Oh, so you already have the button? Are you using OnGUI() for the button?
     
  6. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    I have a button that i created with a plane. I went with that because i wanted the button to only show up when the image target is visible and so it will follow the image target
     
  7. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Do you have it setup to trigger when it gets pressed?
     
  8. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    no
     
  9. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Do you have the button appearing only when you want already?

    If so, how are you toggling it?
     
  10. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    Yes the button already appears when the image target is visible. all i have to do is make it a child of the ImageTarget just like i did with the 3d model
     
  11. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Do you know how it does that, deactivating/activating the GameObject?
     
  12. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    no i don't

    i think it is in this code
     
  13. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    also i have been trying to go the raycast route but i can't get a working code
     
  14. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Ok, I got a much better way to go.

    Can you find the "DefaultTrackableEventHandler.cs" file and paste the source in here for me.
     
  15. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    That is music to my ears right now!

     
  16. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Is there an instance of that attached to you trackable image gameobject?

    If so, then that is what is controlling the child objects.
     
  17. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    yes that is attached under the inspector of the ImageTarget
     
  18. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    how will i make this happen? and will i use the previewanimation script you game me before?
     
  19. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Ok, So, we are going to replace the DefaultTrackableEventHandler.cs with a custom TackableEventHandler.
    First, create a copy of the DefaultTrackableEventHandler.cs file and place it somewhere outside the Vuforia library in your assets, so we know it didn't come with the Vuforia package.
    Then, rename it to something like <customNameHere>TrackableEventHandler.cs and rename the class in the file to the same thing, minus the ".cs" of course.
     
  20. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    check
     
  21. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    What did you rename it? I'm almost done with the script.
     
  22. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    StardustTrackableEventHandler
     
  23. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Save this into the file:

    Code (csharp):
    1. //Modified By: Michanikos(spinnernicholas@gmail.com) 12/05/2013
    2. //From DefaultTrackableEventHandler.cs
    3.  
    4. using UnityEngine;
    5.  
    6. /// <summary>
    7. /// A custom handler that implements the ITrackableEventHandler interface.
    8. /// </summary>
    9. public class StardustTrackableEventHandler : MonoBehaviour,
    10. ITrackableEventHandler
    11. {
    12.     public Previewanimation animatedObject;
    13.     public Rect buttonPosition;
    14.     public string buttonString;
    15.  
    16.     #region PRIVATE_MEMBER_VARIABLES
    17.  
    18.     private bool isTracking = false;
    19.     private TrackableBehaviour mTrackableBehaviour;
    20.  
    21.     #endregion // PRIVATE_MEMBER_VARIABLES
    22.  
    23.  
    24.  
    25.     #region UNTIY_MONOBEHAVIOUR_METHODS
    26.  
    27.     void Start()
    28.     {
    29.     mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    30.     if (mTrackableBehaviour)
    31.     {
    32.     mTrackableBehaviour.RegisterTrackableEventHandler( this);
    33.     }
    34.     }
    35.  
    36.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    37.  
    38.  
    39.  
    40.     #region PUBLIC_METHODS
    41.  
    42.     /// <summary>
    43.     /// Implementation of the ITrackableEventHandler function called when the
    44.     /// tracking state changes.
    45.     /// </summary>
    46.     public void OnTrackableStateChanged(
    47.         TrackableBehaviour.Status previousStatus,
    48.         TrackableBehaviour.Status newStatus)
    49.     {
    50.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    51.             newStatus == TrackableBehaviour.Status.TRACKED)
    52.         {
    53.             isTracking = true;
    54.             OnTrackingFound();
    55.         }
    56.         else
    57.         {
    58.             isTracking = false;
    59.             OnTrackingLost();
    60.         }
    61.     }
    62.  
    63.     #endregion // PUBLIC_METHODS
    64.  
    65.  
    66.  
    67.     #region PRIVATE_METHODS
    68.    
    69.     void OnGUI()
    70.     {
    71.         if(isTracking)
    72.         {
    73.             if(GUI.Button(buttonPosition, buttonString)
    74.             {
    75.                 animatedObject.PreviewAnimation("stardust_waiting", "stardust_attack");
    76.             {
    77.         }
    78.     }
    79.  
    80.     private void OnTrackingFound()
    81.     {
    82.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    83.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    84.  
    85.         // Enable rendering:
    86.         foreach (Renderer component in rendererComponents)
    87.         {
    88.             component.enabled = true;
    89.         }
    90.  
    91.         // Enable colliders:
    92.         foreach (Collider component in colliderComponents)
    93.         {
    94.             component.enabled = true;
    95.         }
    96.  
    97.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    98.     }
    99.  
    100.  
    101.     private void OnTrackingLost()
    102.     {
    103.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    104.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    105.  
    106.         // Disable rendering:
    107.         foreach (Renderer component in rendererComponents)
    108.         {
    109.             component.enabled = false;
    110.         }
    111.  
    112.         // Disable colliders:
    113.         foreach (Collider component in colliderComponents)
    114.         {
    115.             component.enabled = false;
    116.         }
    117.  
    118.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    119.     }
    120.  
    121.     #endregion // PRIVATE_METHODS
    122. }
     
  24. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    After that remove the DefaultTrackableEventHandler and replace it with this new StardustTrackableEventHandler.

    Make sure my other script is attached to the GameObject with the animation object.

    Back in the StardustTrackableEventHandler properties in the inspector, select the object with my other script for Animated Object property, set button position to where you want the button to appear and set button string to what you want the button to say.

    Then, tell me if there are any errors.
     
  25. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    there is no extra options in the inspector for that script. and here are the errors

    Assets/Scripts/stardust/StardustTrackableEventHandler.cs(147,28): error CS1525: Unexpected symbol `{'

    Assets/Scripts/stardust/StardustTrackableEventHandler.cs(159,31): error CS1525: Unexpected symbol `private'

    Assets/Scripts/stardust/StardustTrackableEventHandler.cs(141,17): warning CS0642: Possible mistaken empty statement
     
    Last edited: Dec 5, 2013
  26. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    I forgot to mention, paste it in replacing all the original text.

    I should have just had you create a new script, instead of copying it over.
     
  27. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    well thats what i did...and under the previewanimation script it says the associated script cannot be loaded
     
  28. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    any luck?
     
  29. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Wait, what do you mean under it? In the inspector?
     
  30. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    That's as much as I can do tonight. I'll pick it up again tomorrow.
     
  31. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    Yes under the inspector
     
  32. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    You can try removing the previewanimation script from you animation object and then adding it back.
     
  33. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    Ok I will try that
     
  34. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    It won't let me until i fix those errors
     
  35. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    The same errors from before? It's weird that the line numbers on the errors don't line up to the code I sent you.
     
  36. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    it looks like the copy and pasted might of added in more spaces. It matches up with my code. want to send me the cs file to make sure nothing gets lost in the transfer?
     
  37. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    here is the first error

    and here is the 2nd
     
  38. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Fixed one error:

    Code (csharp):
    1.  
    2. //Modified By: Michanikos(spinnernicholas@gmail.com) 12/05/2013
    3. //From DefaultTrackableEventHandler.cs
    4.  
    5. using UnityEngine;
    6.  
    7. /// <summary>
    8. /// A custom handler that implements the ITrackableEventHandler interface.
    9. /// </summary>
    10. public class StardustTrackableEventHandler : MonoBehaviour,
    11. ITrackableEventHandler
    12. {
    13.     public Previewanimation animatedObject;
    14.     public Rect buttonPosition;
    15.     public string buttonString;
    16.  
    17.     #region PRIVATE_MEMBER_VARIABLES
    18.  
    19.     private bool isTracking = false;
    20.     private TrackableBehaviour mTrackableBehaviour;
    21.  
    22.     #endregion // PRIVATE_MEMBER_VARIABLES
    23.  
    24.  
    25.  
    26.     #region UNTIY_MONOBEHAVIOUR_METHODS
    27.  
    28.     void Start()
    29.     {
    30.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    31.         if (mTrackableBehaviour)
    32.         {
    33.             mTrackableBehaviour.RegisterTrackableEventHandler( this);
    34.         }
    35.     }
    36.  
    37.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    38.  
    39.  
    40.  
    41.     #region PUBLIC_METHODS
    42.  
    43.     /// <summary>
    44.     /// Implementation of the ITrackableEventHandler function called when the
    45.     /// tracking state changes.
    46.     /// </summary>
    47.     public void OnTrackableStateChanged(
    48.         TrackableBehaviour.Status previousStatus,
    49.         TrackableBehaviour.Status newStatus)
    50.     {
    51.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    52.             newStatus == TrackableBehaviour.Status.TRACKED)
    53.         {
    54.             isTracking = true;
    55.             OnTrackingFound();
    56.         }
    57.         else
    58.         {
    59.             isTracking = false;
    60.             OnTrackingLost();
    61.         }
    62.     }
    63.  
    64.     #endregion // PUBLIC_METHODS
    65.  
    66.  
    67.  
    68.     #region PRIVATE_METHODS
    69.    
    70.     void OnGUI()
    71.     {
    72.         if(isTracking)
    73.         {
    74.             if(GUI.Button(buttonPosition, buttonString))
    75.             {
    76.                 animatedObject.PreviewAnimation("stardust_waiting", "stardust_attack");
    77.             {
    78.         }
    79.     }
    80.  
    81.     private void OnTrackingFound()
    82.     {
    83.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    84.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    85.  
    86.         // Enable rendering:
    87.         foreach (Renderer component in rendererComponents)
    88.         {
    89.             component.enabled = true;
    90.         }
    91.  
    92.         // Enable colliders:
    93.         foreach (Collider component in colliderComponents)
    94.         {
    95.             component.enabled = true;
    96.         }
    97.  
    98.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    99.     }
    100.  
    101.  
    102.     private void OnTrackingLost()
    103.     {
    104.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    105.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    106.  
    107.         // Disable rendering:
    108.         foreach (Renderer component in rendererComponents)
    109.         {
    110.             component.enabled = false;
    111.         }
    112.  
    113.         // Disable colliders:
    114.         foreach (Collider component in colliderComponents)
    115.         {
    116.             component.enabled = false;
    117.         }
    118.  
    119.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    120.     }
    121.  
    122.     #endregion // PRIVATE_METHODS
    123. }
    124.  
     
  39. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    now there is 3 errors
     
  40. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Ok, try this:

    Code (csharp):
    1.  
    2. //Modified By: Michanikos(spinnernicholas@gmail.com) 12/05/2013
    3. //From DefaultTrackableEventHandler.cs
    4.  
    5. using UnityEngine;
    6.  
    7. /// <summary>
    8. /// A custom handler that implements the ITrackableEventHandler interface.
    9. /// </summary>
    10. public class StardustTrackableEventHandler : MonoBehaviour,
    11. ITrackableEventHandler
    12. {
    13.     public Previewanimation animatedObject;
    14.     public Rect buttonPosition;
    15.     public string buttonString;
    16.  
    17.     #region PRIVATE_MEMBER_VARIABLES
    18.  
    19.     private bool isTracking = false;
    20.     private TrackableBehaviour mTrackableBehaviour;
    21.  
    22.     #endregion // PRIVATE_MEMBER_VARIABLES
    23.  
    24.  
    25.  
    26.     #region UNTIY_MONOBEHAVIOUR_METHODS
    27.  
    28.     void Start()
    29.     {
    30.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    31.         if (mTrackableBehaviour)
    32.         {
    33.             mTrackableBehaviour.RegisterTrackableEventHandler( this);
    34.         }
    35.     }
    36.  
    37.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    38.  
    39.  
    40.  
    41.     #region PUBLIC_METHODS
    42.  
    43.     /// <summary>
    44.     /// Implementation of the ITrackableEventHandler function called when the
    45.     /// tracking state changes.
    46.     /// </summary>
    47.     public void OnTrackableStateChanged(
    48.         TrackableBehaviour.Status previousStatus,
    49.         TrackableBehaviour.Status newStatus)
    50.     {
    51.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    52.             newStatus == TrackableBehaviour.Status.TRACKED)
    53.         {
    54.             isTracking = true;
    55.             OnTrackingFound();
    56.         }
    57.         else
    58.         {
    59.             isTracking = false;
    60.             OnTrackingLost();
    61.         }
    62.     }
    63.  
    64.     #endregion // PUBLIC_METHODS
    65.  
    66.  
    67.  
    68.     #region PRIVATE_METHODS
    69.    
    70.     void OnGUI()
    71.     {
    72.         if(isTracking)
    73.         {
    74.             if(GUI.Button(buttonPosition, buttonString))
    75.             {
    76.                 animatedObject.PreviewAnimation("stardust_waiting", "stardust_attack");
    77.             }
    78.         }
    79.     }
    80.  
    81.     private void OnTrackingFound()
    82.     {
    83.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    84.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    85.  
    86.         // Enable rendering:
    87.         foreach (Renderer component in rendererComponents)
    88.         {
    89.             component.enabled = true;
    90.         }
    91.  
    92.         // Enable colliders:
    93.         foreach (Collider component in colliderComponents)
    94.         {
    95.             component.enabled = true;
    96.         }
    97.  
    98.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    99.     }
    100.  
    101.  
    102.     private void OnTrackingLost()
    103.     {
    104.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    105.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    106.  
    107.         // Disable rendering:
    108.         foreach (Renderer component in rendererComponents)
    109.         {
    110.             component.enabled = false;
    111.         }
    112.  
    113.         // Disable colliders:
    114.         foreach (Collider component in colliderComponents)
    115.         {
    116.             component.enabled = false;
    117.         }
    118.  
    119.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    120.     }
    121.  
    122.     #endregion // PRIVATE_METHODS
    123. }
    124.  
     
    Last edited: Dec 6, 2013
  41. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    more errors
     
  42. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    I found another error and tried to edit my post before you copied it. Re-copy it from the last post I did.
     
  43. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    error
     
  44. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    in the Previewanimation class, put public before the "class Previewanimation":

    public class Previewanimation........
     
  45. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    no errors but no button shows up
     
  46. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Is all the tracking is working?
     
  47. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    everything is working just as it was before we did any coding
     
  48. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    And, what are your values for:

    buttonPosition
    and
    buttonString
     
  49. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    531
    sorry forgot that part. but the button doesn't follow the image target and when i press the button it loads the idle loop not the attack animation
     
  50. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Wait, so does the model just do nothing and then when you hit the button it does the idle animation and then stops?