Updated on
May 2, 2024
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.
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, labelled 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.
Now we want the green box to move around the four corners of the table as soon as the VR app is started.
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.
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. 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, do the same for each box target: box target 2, box target 3, and box target 4. The final result looks like this:
Although it is possible to create consecutive steps using the Move Object behavior, it is much easier to use a behavior sequence. A behavior sequence is a behavior itself and allows you to add multiple behaviors in sequential order. You can even combine behavior sequences, but usually one behavior sequence is sufficient.
To create the behavior sequence for the first step, select it 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.
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 clicking the 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 has no condition, it will not be repeated.
To change this, let's add a Touch Object condition and reference the green box. If you need additional guidance to implement the Touch Object condition please refer to this tutorial. The final result looks like this:
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.
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 a certain condition can occur. In other words, when 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 we can touch it and thus exit the step.
The Wait for completion feature is activated by ticking the checkbox of the Behavior Sequence in the Step Inspector.
In this tutorial, you learned that behavior sequences are better suited for creating consecutive steps compared to 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!