Create Snap Interactions
Updated: Apr 11, 2024
This tutorial demonstrates how to use snap interactions from the Interaction SDK to snap an object to hands, controllers, controller driven hands, and locations in your Unity scene.
To try snap interactions in a pre-built scene, see the
SnapExamples scene.
The snap interaction is built on top of the grab interaction. Unlike other interactions, the
SnapInteractor is attached to the snappable object instead of your hands or controllers, and the SnapInteractable is attached to the snap location.
Open the Unity scene where you added a grab interaction to a GameObject as mentioned in the prerequisite section.
Under Hierarchy, select the grabbable GameObject you made. Ensure you’re selecting the parent object and not the ISDK_HandGrabInteraction child GameObject.
- Under Inspector, in the Transform component, set the Position field to these values so you can reach the object once you run your scene.
- Set the Scale field to these values so the object is small enough to grab.
Under Inspector, in the Collider component (ex. Box Collider for a cube, Sphere Collider for a sphere), enable the Is Trigger checkbox. This allows the object to hover a snap location by sending information to the SDK when a collision occurs.
Under Hierarchy, select the grabbable GameObject and create an empty child named SnapInteractor by right-clicking in the Hierarchy panel and then clicking Create Empty.
Your hierarchy should look like this.
Select the SnapInteractor GameObject.
Under Inspector, add a Snap Interactor component by clicking the Add Component button and searching for Snap Interactor.
In the Snap Interactor component, set Pointable Element to the ISDK_HandGrabInteraction GameObject, which was auto-generated before this tutorial when you used QuickActions to make the object grabbable.
Pointable Element tells snappable objects where the snap location is so they can move to it during a snap.
Build and run the scene, or if you have a Link connected, click Play.
The world is empty except for your hands and the object. The object will snap to your hand or controller when you grab near it.
Snap interactions can only occur within an area you define using a GameObject that has a rigidbody and a collider. In the
SnapExamples scene, this GameObject is called
Cube. Any snappable locations or objects outside of the collider won’t respond to snap interactions.
Under Hierarchy, add an empty GameObject named SnapLocationRigidBody, which will define the area in your scene where snap interactions can happen.
- Under Inspector, add these components.
In the Rigidbody component, deselect the Use Gravity checkbox, otherwise snapping won’t work because the snappable zone will fall out of reach.
Select the Is Kinematic checkbox so snap locations detect hovering snappable objects.
- Under Inspector, in the Transform component, set the Position field to these values so the collider is placed at the world origin.
- Set the Scale field to these values so the collider is large enough to encompass the snappable object.
A snap location is where your object can snap when you release it. You can have multiple snap locations in a scene.
Under Hierarchy, add an empty GameObject named SnapLocation.
Your hierarchy should look like this.
Under Inspector, add a Snap Interactable component.
In the Snap Interactable component, set the Rigidbody property to SnapLocationRigidBody.
SnapLocationRigidBody uses its collider and rigidbody to define the space where snap interactions can happen. So if you add more snappable objects later on, all of them can use this rigidbody for their Rigidbody property.
In the Project window’s search bar, search for ButtonRing, which will be the mesh that visually represent the snap location. Ensure the search filter is set to either All or In Packages, since the default setting only searches your assets.
Drag and drop the ButtonRing mesh from the search results onto the SnapLocation GameObject in the Hierarchy.
The mesh appears in your scene.
If needed, apply a material to the mesh by dragging and dropping a material onto the mesh. This tutorial uses the gold keyMaterial material.
Under Hierarchy, select SnapLocation.
- Under Inspector, in the Transform component, set the Position field to these values so you can reach the snap location once you run your scene.
Prepare to launch your scene by going to File > Build Settings and clicking the Add Open Scenes button.
Your scene is now ready to build.
Select File > Build And Run, or if you have a Link connected, click Play.
The scene loads. The world is completely empty, but if you raise your hands or controllers, they should appear in front of you.
The cube can now snap to the SnapLocation.
- For reference information about snap interactions, see Snap Interactions.
- To try snap interactions in a pre-built scene, see the SnapExamples scene.