Adding a cube

Now we'll add an object to the scene, just a simple cube:

  1. In the Hierarchy, select Create, then 3D Object | Cube (or from the Main Menu, choose GameObject | 3D Object | Cube). This will create a unit-sized cube, one meter on all sides.
  2. Obviously, that's much too big for our target, which is closer to 0.2 units in size. So, over in the Inspector, in the Transform component, modify the Scale to something more manageable, like (0.05, 0.05, 0.05).

Also, the cube appears submerged into the image. Let's move it up so it's sitting on the target instead:

  1. You can do this by selecting and dragging the yellow (vertical) arrow, or in the Inspector, changing the Transform Position to (0, 0.03, 0) as follows:

Finally, we need to parent the object under the ImageTarget.

  1. In the Hierarchy, select and drag the Cube so that it is a child of the ImageTarget, like so:

Note that the order in which we changed the scene is significant. We first scaled and positioned the cube, then parented it under the ImageTarget. At this point, the cube's transform values have changed in the Inspector, although the object looks unchanged on the screen. Why? Because the cube transform has local values relative to its parent transform. Unity makes the adjustment for us when you move things in the Hierarchy! So, we had a cube size of 0.05 and the TargetImage object size of 0.25; now as a child object, the cube's local size changed to 0.2, but its world size is still 0.05 (0.2 times 0.25 equals 0.05).

If you don't completely get this, don't worry; you'll get used to it, and usually the hard numbers are less important than how things look on the screen. The Scene should now look something like this:

Save the Scene and project:

  1. From the Main Menu, click File | Save Scene.
  2. Then File | Save Project.

Now we're ready to see it work. Press Play again in the Unity toolbar. Point your webcam at the stones image. Our cube should appear. Woot!!!

The following image shows me pointing my webcam at the target pebbles image, with Unity running the scene on the monitor in the back. In the Game panel, you can see the video feed from the webcam, with the cube added to it, in all its augmented reality glory!

If you also want to try another SDK, ARToolkit, you can follow the next topic, or move on to the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset