Continuing with the example of moving, resizing and rotating objects with the BoundingBox, this article is a sequel to achieving the same objective – but in another easier way.
As I go deeper into exploring the MR Toolkit-Unity, I see that lot of things are made much more easier and intuitive for the user. For instance- if I were to move an object in the real world, I would use two hands.
The TwoHandManipulatable script in the MR Toolkit-Unity is precisely aiming to do this. Furthermore, you can achieve object rotation and scaling (resizing) with this script attached to your object.
I also list a tradeoff of my experiences using the three different options to move the objects. i.e AppBar and BoundingBox, HandDraggable.cs and TwoHandManipulatable.cs
App Bar+ Bounding Box
|Move – not applicable
Scale – separate variable specified for scale. Click and hold on handles can be a little annoying
Rotate – click and hold on handles can be a little annoying to rotate
Pros: clearly specified boundary. And useful if you want specific movements
|only Move possible – scale rate is separate. However not free flowing
Only one hand movement possible
Pros: easy to use, simply attach to the object with a mesh collider
Move, scale and rotate all three are possible
Pros: free flowing, intuitive and two hand movements, simply attach the script and use it
To get started, do the following:
1. Create a new project in Unity
It’s better if you get Unity 2018 version as you’d need to make it compatible with the next must have – Mixed Reality Toolkit- Unity
I’ve also done this with the latest OS Windows 10 Redstone 4 (which is so cool, more updates in the following article)
2. Import the Mixed Reality Toolkit-Unity from https://github.com/Microsoft/MixedRealityToolkit-Unity/releases/tag/2017.4.0.0
3. Do the usual tweaks:
a. Delete the main Camera. Add the MixedRealityCameraParent.prefab in the Hierarchy. This will serve as the Main Camera
b. Change the Camera Settings to Hololens preferred:
Skybox to Solid Color
Background to 0,0,0,0
c. Go to Edit ->Project Settings -> Player -> Other Settings -> and change the Scripting Runtime version to Experimental
d. Save the scene
Now let’s get some objects within the scene which we can manipulate:
Let’s reuse the Furniture Pack 3D Asset pack from the Asset Store for free from the last project itself https://assetstore.unity.com/packages/3d/props/furniture/pack-gesta-furniture-1-28237
I’m going to add the objects: tv1, glass_table and bed2. You can add any objects that you like or add your own
Just so that it is organized, I will create an empty GameObject and move my objects into the scene. I called this object MovableGameObjects
Also ensure that the scale of the objects are smaller, since otherwise they are going to look huge!
I changed the scale of all three objects to: 0.5, 0.5, 0.5
Also make sure you adjust the camera position to an optimal view of the objects
If all is done correctly so far, then you will see the three objects grouped under MovableGameObjects
Now for the part where we can interact with the movable objects using the TwoHandManipulatable.cs script
1. First add the Default Cursor.prefab from the HoloToolkit. Search for it in the Assets and drag and drop it into the Hierarchy
2. Search for the InputManager.prefab in the Assets and drag and drop it into the Hierachy as well
3. On the Inspector settings of the InputManager, under SimpleSingerPointerSelector component, drag and drop the Default Cursor into the Cursor field (if you haven’t seen the article Gaze and Tap on objects, then this is a good time to refer to it)
4. For every object that you want to interact with, you need to ensure that there is a corresponding MeshCollider. I added Mesh Colliders to the individual objects which are movable. In my experience, the MeshColliders work best for this kind of gesture input
So click on every movable object and in the Inspector’s Add Component, add a Mesh Collider
(Note that the asset may already come with it’s own Mesh Collider, so you don’t need to add an extra one in this case)
Coming to the twohandmanipulation part:
In the Assets, search for the TwoHandManipulatable.cs script and add it to any object.
Let’s add it to the MovableObjects
Make sure you change the manipulation mode to Move, Scale and Rotate if you want all three interactions
If you want to attach a bounding box to the objects so that you know when it’s in the manipulation mode, then simply drag and drop the BoundingBoxBasic.prefab into the Bounding Box Prefab Inspector variable of the TwoHandManipulatable script.
Build and deploy onto the HoloLens and let’s see if the interactions work.
I’ve attached a video of how the interactions should look like
Let’s look at the Inspector variables of the TwoHandManipulatable.cs script.
So what is important for us is 4 different variables:
Bounding Box Prefab is the blue box around the object as you saw in the video. It’s optional, so you can simply leave this field empty if you don’t want it. I use it mainly for visual feedback
Manipulation Mode can be different values. If you want all three Move, Rotate and Scale then choose that
Rotation Constraint can be limited to any Axis
Enable One Hand Movement is checked when you want to move objects with just one pinch and drag
Errors and solutions:
Error 1: Using the MR Toolkit-Unity may cause some errors. So if you see the errors shown below, then you need to change only one thing
Any of your scripts may show this error
Solution: First check if you have imported the HoloToolkit properly, else, see if the Console is showing this error
For this, go to Edit ->Project Settings -> Player -> Other Settings -> and change the Scripting Runtime version to Experimental
Hope this article has helped you seamlessly move, scale and rotate objects. Let me know in your comments !