Object Targets enables you to create apps that can recognize and track physical, 3D objects based on their visual features, such as toys. This article will show you how to add Object Targets (3D scanned) to a Unity project, and how to customize the behaviors exposed through the Object Target Behaviour component.
If you are unfamiliar with the Unity Workflow, it may be helpful to read Getting Started with the Unity Workflow
You will also need to get a license key from the License Manager and add it to your project if you are looking to track your own objects. The following articles may be helpful: How To Create an App License and How To add a License Key to your Vuforia App
Adding Object Targets to Your Project
The workflow for adding Object Targets to a Vuforia Engine Unity project is very similar to that of other Vuforia features. Firstly, set up your Unity project with the latest Vuforia Engine SDK with the Vuforia Engine Package for Unity.
To use your own Object Target, please refer to Vuforia Object Scanner and How to Work with Device Databases for steps to create your own Object Target and storing it in a downloaded database. This guide can also be done by using the sample Object target database.
- In your Unity scene, add an ARCamera GameObject instance to your scene. Remove the default Main Camera from your scene.
- Import your device database with an Object Target as a .unitypackage from Assets -> Import Package -> Custom Package.
- As an alternative, you can import the default Object Target database with a Mars Habitat if Vuforia detects no database was added yet. Simply add the Object Target GameObject as described in step 3 and press Import in the pop-up window.
- Create an ObjectTarget (3D Scanned) GameObject from the menu GameObject -> Vuforia Engine -> Object Target.
- Select the ObjectTarget and configure its Database and Object Target that you want to associate with the Object Target instance.
- Add your content as a child to the Object Target GameObject.
- In the Advanced expandable menu, you can enable the bounding box to position and scale your content in relation to the target.
- Also select the motion hint for your Object Target that best fit your use case. The motion hint can increase tracking stability in scenarios where the object is moved or when it is stationary. See Motion Hints for more details.
- Test your scene in Play Mode by pressing the Play button in the top panel of the Editor.
The Object Target bounding box corresponds to the grid region of the Object Target. You can use the Object Scanning target as a physical reference while in Play Mode to help position your content as illustrated in the following image setup:
Important: Making changes while in Play Mode will not be saved! Exit Play Mode every time you wish to make changes.
Create an Object Target Instance
The ObjectTargetBehaviour tracks the Object Targets. For a full overview of its capabilities, please see ObjectTargetBehaviur reference library.
The Object Target is started and stopped with the ObserverBehaviour.
objectTarget.enabled = true; objectTarget.enabled = false:
Customizing Event Handling
Object Targets can have multiple detection and tracking states as defined by the Status of their TargetStatus. The status of a target enables you to develop apps that unlock content in relation to how well it is being tracked.
You can use result of Status and StatusInfo to determine when an Object Target is recognized, when it is being actively tracked, and when tracking and detection have been lost on an object that was previously being tracked. Use the events to disable Renderers and Colliders and assign your own event handling to the Default Observer Event Handler.
Activating Loaded Datasets at Runtime
It is also possible to load and disable your Object Targets at runtime if you for example wish to switch between targets from the same database. See How to Create and Load Targets in Unity for a guide on implementing runtime behaviors in your Unity project.
Moving Objects during Tracking
Vuforia Engine only supports limited tracking on moving objects, provided the motion is slow and smooth. To best support Vuforia with tracking objects in movement, you will need to disable the Device Pose trackingin VuforiaConfiguration. Set the Object Target’s motion hint to a scenario best fitting your use case. See Motion Hints for details.
Device tracking can also be started and stopped during runtime by accessing the DeviceBehaviour class and enable it.
Please see the following article for more information about device tracking: Device Tracking in Unity.
Object Recognition in Native