Overview
This article describes how to use State Machines to toggle different properties on Unity game objects in sequence without using Storyflow.
In this example, we will use them to simply toggle different features of certain game objects in a predefined sequence.
We will do this using two premade Motive SDK components.
VR Grabbable
State Machine Driver
For this example, we are using the premade Motive Training Workshop. However, this same example can be done using any two grabbable game objects that use the Motive VRGrabbable component.
Setup
The first thing to do is to make a folder in your Unity projects Project window and name it “State Machines”
Open the newly made folder and right-click to add a State Machine by selecting Create>Motive>State Machine
In the Initial State text box, we are going to write “Nothing Grabbed”
Next in the textbox next to the States dropdown list, we are going to write in “3”
This will create three new States that you can open by expanding their dropdown lists. Afterwards, we will name them “Nothing Grabbed,” “Hammer Grabbed,” and “Wrench Grabbed”
After that, we will expand the Transitions dropdown and hit the + icon shown for the first two of the three states
This will now reveal another set of dropdowns for the newly made Elements. With the new dropdowns, we will open them and then expand another dropdown titled Event Counts
In the expanded Events Counts dropdown, we will hit the + icon again
After that, we will be met with yet another dropdown for the newly made Elements. Expand this last dropdown to reveal the new Event and Count textboxes
For the “Nothing Grabbed” Event textbox, type in “Grab Hammer” and set the Count to 1.
And further below that, set the To State textbox to “Hammer Grabbed”
For the “Hamme Grabbed” Event textbox, type in “Grab Wrench” and set the Count to 1.
And further below that, set the To State textbox to “Wrench Grabbed”
By the end, your State Machine should look like the image below
Once you have completed your State Machine, we will start configuring our Game Objects. For this example, we will only be utilizing the 3 highlighted objects in the Scene Hierarchy.
The first thing we need to do is set up the Tools object.
The first component we will add is a State Machine Driver
Next, we need to hit the Target icon next to the State Machine box and select your newly made State machine
Afterwards, click the checkbox titled Import States From Machine
This will automatically create 3 new State Behaviours. Expand each of these behaviours so that you can see the empty lists for entering/exiting the states
In the On Enter State () list for the Nothing Grabbed state, hit the plus icon once
Next, where it reads None (Object), drag in the SM_wrench Variant from the Hierarchy
Then, select the No Function dropdown and select VRGrabbable>DisableGrab
Next, for the On Enter State () list for the Hammer Grabbed state, hit the plus icon once
Next, where it reads None (Object), drag in the SM_wrench Variant from the Hierarchy
Then, select the No Function dropdown and select VRGrabbable>EnableGrab
Next, for the On Enter State () list for the Wrench Grabbed state, hit the plus icon once
Next, where it reads None (Object), drag in the SM_hammer Variant from the Hierarchy
Then, select the No Function dropdown and select MeshRenderer>bool enabled
By the end of these changes, your State Machine Driver component should look something like this
Next, we need to add the FireEvent calls to the VRGrabbable components on both the wrench and the hammer
First, we will do the hammer. So find SM_hammer Variant in the Hierarchy and then expand the VRGrabbable component
Next, hit the + icon on the On Grab begin list.
Then, where it reads None (Object), drag in the Tools object from the Hierarchy
Then, select the No Function dropdown and select StateMachineDriver>FireEvent, and in the newly made textbox, write “Grab Hammer”
Then find SM_wrench Variant in the Hierarchy and expand the VRGrabbable component
Next, hit the + icon on the On Grab begin list.
Then, where it reads None (Object), drag in the Tools object from the Hierarchy
Then, select the No Function dropdown and select StateMachineDriver>FireEvent, and in the newly made textbox, write “Grab Wrench”
Save the scene using ctrl+S
Final Product
At the end of this example, we are left with a scene where the SM_hammer Variant is grabbable while the SM_wrench Variant is not. However, using State Machines, we are able to enable the grab on the wrench by grabbing the hammer, then disabling the visibility of the hammer once the wrench is grabbed. All without needing to author in Storyflow.