7. Interacting with 3D objects

In this tutorial, you will learn how to enable near and far manipulation of 3D objects and limit the allowed types of manipulation. You will also learn how to add bounding boxes around 3D objects to make it easier to control the object manipulation.

Objectives

  • Learn how to configure 3D objects so they can be interacted with
  • Learn how to add bounding boxes to 3D objects

Manipulating 3D objects

In this section, you will add the ability to manipulate all the Rover parts you organized on the table during the Positioning objects in the scene tutorial.

The main steps you will take to achieve this are:

  1. Add the Object Manipulator (Script) component to all the part objects
  2. Add the NearInteractionGrabbable component to all the part objects
  3. Configure the Object Manipulator (Script) component

Note

To be able to manipulate an object, the object must have the following components:

  • Collider component, for example, a Box Collider
  • Object Manipulator (Script) component

To be able to manipulate and grab an object with tracked hands, the object must have the following components:

  • Collider component, for example, a Box Collider
  • Object Manipulator (Script) component
  • NearInteractionGrabbable component

Additionally, you will configure the Rover Explorer so that you can place the rover parts on to the Rover to make it a complete rover assembly.

In the Hierarchy window, expand the RoverExplorer > RoverParts object and select all its child rover part objects and the RoverAssembly object, then in the Inspector window, use the Add Component button to add the following components to all the selected objects:

  • Object Manipulator (Script) component
  • NearInteractionGrabbable component
  • Part Assembly Controller (Script) component

Unity with RoverAssembly and all rover part objects selected and components added

Tip

To select multiple objects that are not next to each other, press-and-hold the CTRL key while using the mouse to select any object.

Note

For the purpose of this tutorial, colliders have already been added to the rover parts. To learn more about colliders, you can visit Unity's Collider documentation.

Note

The Part Assembly Controller (Script) is not part of the MRTK but was included with the tutorial assets.

With all the rover part objects and the RoverAssembly object still selected, in the Inspector window, configure the Object Manipulator (Script) component as follows:

  • From the Two Handed Manipulation Type dropdown, uncheck the Scale, so only Move and Rotate is enabled

Unity with Two Handed Manipulation Type configured

Note

At this point, you have enabled object manipulation for all the rover part objects and the RoverAssembly object.

In the Project window, navigate to the Assets > MRTK > SDK > StandardAssets > Audio folder to locate the audio clips:

Unity Project window with Audio folder selected

In the Hierarchy window, reselect all the rover part objects, then in the Inspector window, use the Add Component button to add the Audio Sources component and configure it as follows:

  • Assign the MRTK_Scale_Start audio clip to the AudioClip field
  • Uncheck the Play On Awake checkbox
  • Change Spatial Blend to 1

Unity with all rover parts selected and Audio Source component added and configured

In the Hierarchy window, expand the RoverAssembly > RoverModel_PlacementHints_XRay > Parts_PlacementHints object to reveal all of the placement hint objects, then select the first rover part, RoverParts > Camera_Part, and configure the Part Assembly Controller (Script) component as follows:

  • Assign the Camera_PlacementHint object to the Location To Place field

Unity with Camera_Part PartAssemblyController component configured

Repeat this step for each of the remaining rover part objects and the RoverAssembly object to configure the Part Assembly Controller (Script) component as follows:

  • For the Generator_Part, assign the Generator_PlacementHint object to the Location To Place field
  • For the Lights_Part, assign the Lights_PlacementHint object to the Location To Place field
  • For the UHFAntenna_Part, assign the UHFAntenna_PlacementHint object to the Location To Place field
  • For the Spectrometer_Part, assign the Spectrometer_PlacementHint object to the Location To Place field
  • For the RoverAssembly, assign the object itself, i.e. the same RoverAssembly object, to the Location To Place field

In the Hierarchy window, select the RoverExplorer > Buttons > Reset button object, then in the Inspector window, configure the Interactable OnClick () event as follows:

  • Assign the RoverAssembly object to the None (Object) field
  • From the No Function dropdown, select PartAssemblyController > ResetPlacement () to set this function as the action to be executed when the event is triggered

Unity with Reset button object OnClick event configured

If you now enter Game mode, you can use near or far interaction to place the rover parts on to the Rover. Once the part is close to the corresponding placement hint, it will snap into place and become part of the Rover. To reset the placements, you can press the Reset button:

Unity Play mode split view with Reset button being pressed

To learn more about the Object Manipulator component and its associated properties, you can visit the Object Manipulator guide in the MRTK Documentation Portal.

Adding bounding boxes

Bounding boxes make it easier and more intuitive to manipulate objects with one hand for both near and far interaction by providing handles that can be used for scaling and rotating.

In this example, you will add a bounding box to the RoverExplorer object so the whole experience can easily be moved, rotated, and scaled. Additionally, you will configure the Menu so you can turn the Bounding Box on and off.

In the Hierarchy window, select the RoverExplorer object, then in the Inspector window, use the Add Component button to add the following components:

  • BoundingBox component
  • Object Manipulator (Script) component

Then uncheck the checkbox next to both components to make them disabled by default:

Unity with RoverExplorer object selected and components added and disabled

Note

The Bounding Box visualization is created at runtime and, therefore, not visible before you enter Game mode.

Note

The BoundingBox component will automatically add the NearInteractionGrabbable component at runtime. Therefore, we do not need to add this component to grab the enclosed objects with tracked hands.

In the Hierarchy window, expand the Menu > ButtonCollection object to reveal the four buttons and rename the third button to BoundingBox_Enable, then in the Inspector window, configure the Button Config Helper (Script) component as follows:

  • Change the Main Label Text to Enable
  • Assign the RoverExplorer object to the None (Object) field
  • From the No Function dropdown, select BoundingBox > bool Enabled to update this property value when the event is triggered
  • Verify that the argument checkbox is checked
  • Click the small + icon to add another event
  • Assign the RoverExplorer object to the None (Object) field
  • From the No Function dropdown, select ObjectManipulator > bool Enabled to update this property value when the event is triggered
  • Verify that the argument checkbox is checked
  • Leave the Icon as the 'cube with bounding box' icon

Unity with BoundingBox_Enable button object selected and Button Config Helper component configured

Rename the forth and last button to BoundingBox_Disable, then in the Inspector window, configure the Button Config Helper (Script) component as follows:

  • Change the Main Label Text to Disable
  • Assign the RoverExplorer object to the None (Object) field
  • From the No Function dropdown, select BoundingBox > bool Enabled to update this property value when the event is triggered
  • Verify that the argument checkbox is unchecked
  • Click the small + icon to add another event
  • Assign the RoverExplorer object to the None (Object) field
  • From the No Function dropdown, select ObjectManipulator > bool Enabled to update this property value when the event is triggered
  • Verify that the argument checkbox is unchecked
  • Change the Icon to the 'cube with bounding box" icon

Unity with BoundingBox_Disable button object selected and Button Config Helper component configured

If you now enter Game mode and enable the Bounding Box by clicking the Enable button, you can use near or far interaction to move, rotate, and scale the Bounding Box, and use the Disable button to disable the Bounding Box again:

Unity Play mode split view with Bounding Box being manipulated

To learn more about the Bounding Box component and its associated properties, you can visit the Bounding box guide in the MRTK Documentation Portal.

Congratulations

In this tutorial, you learned how to enable near and far manipulation for 3D objects and how to limit the allowed types of manipulation. You also learned how to add bounding boxes around 3D objects to make it easier to control the object manipulation.