Using State Machines to Change Properties
In depth learning on Usable World Objects can be found in this course in Motive Academy.
- 1 Overview
- 1.1 Setup
- 1.2 Final Product
- 2 Related Articles
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
Open the “Project” window and create a new folder named State Machines
Right-click on the folder to add a State Machine by selecting Create>Motive>State Machine
In the Initial State text box, type Nothing Grabbed
In the textbox next to the States dropdown list, type in 3
This will create three new States that you can open by expanding their dropdown lists.
Name the new states: Nothing Grabbed, Hammer Grabbed, and Wrench Grabbed
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.
Open the new dropdowns and then expand another dropdown titled Event Counts
In the expanded Events Counts dropdown, choose the + icon again
You will now see 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
Set the To State textbox to Hammer Grabbed
For the “Hammer Grabbed” Event textbox, type in Grab Wrench and set the Count to 1
Below that, set the To State textbox to Wrench Grabbed
By the end, your State Machine should look like this:
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.
Add a State Machine Driver component
Hit the Target icon next to the State Machine box and select your newly made State machine
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 + icon once
Where it reads None (Object), drag in the SM_wrench Variant from the Hierarchy
Select the No Function dropdown and select VRGrabbable>DisableGrab
For the On Enter State () list for the Hammer Grabbed state, hit the + icon once
Where it reads None (Object), drag in the SM_wrench Variant from the Hierarchy
Select the No Function dropdown and select VRGrabbable>EnableGrab
For the On Enter State () list for the Wrench Grabbed state, hit the + icon once
Where it reads None (Object), drag in the SM_hammer Variant from the Hierarchy
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.
Find SM_hammer Variant in the Hierarchy and then expand the VRGrabbable component
Hit the + icon on the On Grab begin list
Where it reads None (Object), drag in the Tools object from the Hierarchy
Select the No Function dropdown and select StateMachineDriver>FireEvent
In the newly made textbox, add Grab Hammer
Find SM_wrench Variant in the Hierarchy and expand the VRGrabbable component
Hit the + icon on the On Grab begin list
Where it reads None (Object), drag in the Tools object from the Hierarchy
Select the No Function dropdown and select StateMachineDriver>FireEvent
In the newly made textbox, add 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.