Use Active State
In this tutorial, you learn how to use Interaction SDK’s
Active State in two scenarios that you should complete in order. Active State checks for a specified condition and returns a boolean. For example, Active State can tell you if your hands are active, if your controllers are grabbing, or if your hands are matching a specific pose. You can check elements, like hands or controllers, GameObjects, or interactors for their Active State.
This tutorial includes two scenarios that you should complete in order.
Check state of one element
To check the state of an element, you use the corresponding
...Active State component. For example, hands use
HandActiveState, GameObjects use
GameObjectActiveState, and interactors use
InteractorActiveState. In this scenario, you’ll check the state of the grab interactor for the left hand and cause a cube to glow green whenever the state is true.
- In the Project window, search for HandGrabExamples. Ensure the search filter is set to either All or In Packages, since the default setting only searches your assets.
- Open the HandGrabExamples scene. This scene already includes grabbable objects, so you’ll use this scene instead of a new scene to save you time.
- Under Hierarchy, create a Cube GameObject by right-clicking in the Hierarchy and selecting 3D Object > Cube. This cube will visually display the active state by glowing green whenever the active state is true.
- Under Hierarchy, select Cube.
- Under Inspector, in the Transform component, set the Position values as follows.
Set the Scale values to 0.1 for the X, Y, and Z axes.
The cube is now positioned just in front of the Dialog GameObject so you can see it while you’re interacting with the scene’s grabbable objects.
- Add an Interactor Active State component by clicking the Add Component button at the bottom of the Inspector and searching for Interactor Active State. This component will track the state of the grab interactor.
- In the Interactor Active State component, set Interactor to the HandGrabInteractor GameObject, which is located under OVRCameraRig > OVRInteraction > OVRHands > LeftHand > HandInteractorsLeft > HandGrabInteractor.
Set the Property field to Is Selecting, since for this tutorial you only want to detect when the hand is grabbing.
- Add an Active State Debug Visual component, which changes the color of its GameObject when the assigned active state changes.
- In the Active State Debug Visual component, set the Active State field to the Cube GameObject.
Set the Target field to the Cube GameObject so the cube is what changes color when the state changes.
On the menu, go to File > Build Settings, click Add Open Scenes, and then click Build and Run to build the scene and play it in the headset.
Grabbing any item in the scene with your left hand activates the active state, causing the cube to glow green.
Check state of multiple elements
In this scenario, you’ll add to the previous scenario by checking the active state of both hands, not just the left hand. To check the state of a group of elements, you use an Active State Group component. In an Active State Group, each element is evaluated, and then the group is evaluated using its boolean operator to return a final value of either true or false.
- Open the modified HandGrabExamples scene from the previous scenario.
- Under Hierarchy, select Cube.
- Under Inspector, add a second Interactor Active State component.
- In that component, set the Interactor property to the HandGrabInteractor for the right hand, which is located under OVRCameraRig > OVRInteraction > OVRHands > RightHand > HandInteractorsRight > HandGrabInteractor.
- Set the Property property to Is Selecting to detect when the hand is grabbing.
- Add an Active State Group component, which checks the value of its active states and returns a final boolean value.
- In the Active State Group component, add two empty elements to the Active States list.
- Set the elements to the two Interactor Active State components, which are listed in the Inspector above the Active State Group component.
Set the Logic Operator field to OR so that either hand can activate the Active State.
In the Active State Debug Visual component, set the Active State field to the Active State Group component. This causes the cube to monitor and display the final state of the Active State Group.
Select File > Build And Run, or if you have a Link connected, click Play.
Grabbing any item in the scene with either hand or both hands activates the active state, causing the cube to glow green.