Behavior sequence

Updated on

July 27, 2022

In this VR Builder tutorial, you will learn how to create sequential behaviors for your VR application in Unity. Additionally, we'll cover the benefits of using behavior sequences instead of creating sequential steps. To that end, we'll also cover the Repeat and Wait for completion options, two powerful features for shaping the process of your VR application in Unity.

  1. Setting up the scene in Unity
  2. Move Object Behavior vs. Behavior Sequence for creating consecutive steps
  3. Repeating behaviors with the Repeat feature
  4. Conditional termination with the Wait For Completion feature
  5. Next steps

Setting up the scene in Unity

In this Unity scene, there is an environment with a table and a floor. On the table there is a green box. We use basic Unity boxes for simplicity but VR Builder works with any 3D models, as well. In addition, there are four empty game objects, labeled Box Target 1 to Box Target 4. They are located in each of the four corners of the table and will be used as reference positions the green box will move to.

We also create a process with a single step. For additional help, please refer to the tutorial on the VR Builder Workflow Editor. This is the final workflow:

Now we want the green box to move around the four corners of the table as soon as the VR app is started.

Move Object Behavior vs. Behavior Sequence for creating consecutive steps

In this chapter, we will first create the intended effect by using a sequence of steps. Then we will show how to accomplish the same effect by using a behavior sequence and highlight the benefits of this approach.

Creating consecutive steps

To move the green box to Box Target 1, Box Target 2, Box Target 3, and Box Target 4, you can create four steps and apply the Move Object behavior to each. Setting the workflow with a sequence of steps also requires to adding an additional step between the start node and the steps where the boxes are moved. Although this is not overly complicated, the work can become redundant and the workflow messy. For example, you can move the box on the table by adding the Move Object behavior in the Step Inspector > Behaviors > Add Behavior > Animation > Move Object.

Then, refer to the Box as the Object and the Box Target 1 as the final position provider. Set the animation duration to one second.

Next, repeat the same for each box target: Box Target 2, Box Target 3, and Box Target 4. The final workflow looks super messy:

Creating consecutive steps with Behavior Sequence

Although it is possible to create consecutive steps using the Move Object behavior, it is much easier to do so with a behavior sequence. A behavior sequence is a behavior itself and allows you to add multiple behaviors in sequential order. You can even combine several behavior sequences, but usually one behavior sequence is sufficient. To create the behavior sequence for the first step, select the first step and go to Step Inspector > Behaviors > Add Behavior > Utility > Behaviors Sequence.

Now you can add more behaviors to the step, using so-called child behaviors. Again, create the same behaviors for each box target. The final result looks like this:

The advantage of using the behavior sequence instead of several consecutive steps is that even with a large number of behaviors, the workflow remains clean since all is contained in one step.

Repeating Behavior Sequences with the Repeat feature

The repeat feature of the behavior sequence enables the repetition of child behaviors. When enabled, the individual behaviors of the behavior sequence are repeated infinitely until a condition triggers the stop. You can enable the repeat feature by simply activating the Repeat checkbox in the behavior sequence of the Step Inspector.

In our case, this means that if the repeat feature is activated, the box moves to all four target positions on the table. Once it reaches the last box target position, the box continues the procedure from the beginning until all conditions of a transition are fulfilled. Since the step in our example doesn't entail a condition, it will not be repeated.

To make the step repeat, let's add a Touch Object condition by going to the Step Inspector > Transitions > Add Condition > Interaction > Touch Object.

Press the Fix it button to add the necessary scripts. Then reference the Box as the Object to be touched.

If we now start the VR application, the box repeatedly moves across the table. Only when we touch the box, it moves immediately to the final target position (Box Target 4) and stops there. The VR application is terminated.

Conditional termination with the Wait For Completion feature

A powerful complement to the Repeat feature is the Wait for completion feature. This feature makes it impossible to skip behaviors of a step before certain conditions can occur. In other words: once the Wait for completion feature is enabled, the box must complete the first iteration of the behavior sequence, i.e. moves to all four target positions, before the user can touch it in VR and thus exit the step.

You can activate the Wait for completion feature by ticking the checkbox of the behavior sequence in the Step Inspector.

Next steps

In this VR Builder tutorial, you learned that behavior sequences are better suited for creating consecutive steps compared to sequential steps with the Move Object behavior, because it organizes the workflow better. In this context, you also learned about two powerful features of behavior sequences: the Repeat feature and the Wait for completion feature. Together, they make behavior sequences an effective solution for efficiently creating consecutive steps in Unity. To make your application even more engaging for your users in VR, you can also use timed effects, such as delays. Check out our Unity tutorial on delay behaviors and timeout conditions!

Helpful links

Download Vr Builder