Using State Machines to Change Properties

In depth learning on Usable World Objects can be found in this course in Motive Academy.

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

  1. Open the “Project” window and create a new folder named State Machines

  2. Right-click on the folder to add a State Machine by selecting Create>Motive>State Machine

  3. In the Initial State text box, type Nothing Grabbed

  4. 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.

  1. Name the new states: Nothing Grabbed, Hammer Grabbed, and Wrench Grabbed

  2. 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.

  1. Open the new dropdowns and then expand another dropdown titled Event Counts

  2. In the expanded Events Counts dropdown, choose the + icon again

  3. You will now see another dropdown for the newly made Elements. Expand this last dropdown to reveal the new Event and Count textboxes

  4. For the “Nothing Grabbed” Event textbox, type in Grab Hammer and set the Count to 1

  5. Set the To State textbox to Hammer Grabbed

  6. For the “Hammer Grabbed” Event textbox, type in Grab Wrench and set the Count to 1

  7. 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.

  1. Add a State Machine Driver component

  2. Hit the Target icon next to the State Machine box and select your newly made State machine

  3. Click the checkbox titled Import States From Machine

This will automatically create 3 new State Behaviours.

  1. Expand each of these behaviours so that you can see the empty lists for entering/exiting the states

  2. In the On Enter State () list for the Nothing Grabbed state, hit the + icon once

  3. Where it reads None (Object), drag in the SM_wrench Variant from the Hierarchy

  4. Select the No Function dropdown and select VRGrabbable>DisableGrab

  5. For the On Enter State () list for the Hammer Grabbed state, hit the + icon once

  6. Where it reads None (Object), drag in the SM_wrench Variant from the Hierarchy

  7. Select the No Function dropdown and select VRGrabbable>EnableGrab

  8. For the On Enter State () list for the Wrench Grabbed state, hit the + icon once

  9. Where it reads None (Object), drag in the SM_hammer Variant from the Hierarchy

  10. 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.

  1. Find SM_hammer Variant in the Hierarchy and then expand the VRGrabbable component

  2. Hit the + icon on the On Grab begin list

  3. Where it reads None (Object), drag in the Tools object from the Hierarchy

  4. Select the No Function dropdown and select StateMachineDriver>FireEvent

  5. In the newly made textbox, add Grab Hammer

  6. Find SM_wrench Variant in the Hierarchy and expand the VRGrabbable component

  7. Hit the + icon on the On Grab begin list

  8. Where it reads None (Object), drag in the Tools object from the Hierarchy

  9. Select the No Function dropdown and select StateMachineDriver>FireEvent

  10. In the newly made textbox, add Grab Wrench

  11. 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.

Related Articles

Recipes