Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

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. The first thing to do is to make a folder in your Unity projects Project window and name it “State Machines”

  2. Open the newly made folder and right-click to add a State Machine by selecting Create>Motive>State Machine

  3. In the Initial State text box, we are going to write “Nothing Grabbed”

  4. Next in the textbox next to the States dropdown list, we are going to write in “3”

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

  5. After that, we will expand the Transitions dropdown and hit the + icon shown for the first two of the three states

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

      1. In the expanded Events Counts dropdown, we will hit the + icon again

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

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

      1. And further below that, set the To State textbox to “Hammer Grabbed”

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

      1. And further below that, set the To State textbox to “Wrench Grabbed”

    3. By the end, your State Machine should look like the image below

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

  8. The first thing we need to do is set up the Tools object.

    1. The first component we will add is a State Machine Driver

      1. Next, we need to hit the Target icon next to the State Machine box and select your newly made State machine

      2. Afterwards, click the checkbox titled Import States From Machine

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

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

    1. Next, where it reads None (Object), drag in the SM_wrench Variant from the Hierarchy

    2. Then, select the No Function dropdown and select VRGrabbable>DisableGrab

  10. Next, for the On Enter State () list for the Hammer Grabbed state, hit the plus icon once

    1. Next, where it reads None (Object), drag in the SM_wrench Variant from the Hierarchy

    2. Then, select the No Function dropdown and select VRGrabbable>EnableGrab

  11. Next, for the On Enter State () list for the Wrench Grabbed state, hit the plus icon once

    1. Next, where it reads None (Object), drag in the SM_hammer Variant from the Hierarchy

    2. Then, select the No Function dropdown and select MeshRenderer>bool enabled

  12. By the end of these changes, your State Machine Driver component should look something like this

  13. Next, we need to add the FireEvent calls to the VRGrabbable components on both the wrench and the hammer

    1. First, we will do the hammer. So find SM_hammer Variant in the Hierarchy and then expand the VRGrabbable component

      1. Next, hit the + icon on the On Grab begin list.

        1. Then, where it reads None (Object), drag in the Tools object from the Hierarchy

        2. Then, select the No Function dropdown and select StateMachineDriver>FireEvent, and in the newly made textbox, write “Grab Hammer”

    2. Then find SM_wrench Variant in the Hierarchy and expand the VRGrabbable component

      1. Next, hit the + icon on the On Grab begin list.

        1. Then, where it reads None (Object), drag in the Tools object from the Hierarchy

        2. Then, select the No Function dropdown and select StateMachineDriver>FireEvent, and in the newly made textbox, write “Grab Wrench”

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

  • No labels