© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
C. CoutinhoUnity® Virtual Reality Development with VRTK4https://doi.org/10.1007/978-1-4842-7933-5_13

13. Climbing in VR

Christopher Coutinho1  
(1)
GameWorks, Mumbai, Maharashtra, India
 

The ability to climb up objects, Tomb Raider style, to get to your destination is one of the most enthralling things you can do in VR. The VRTK makes climbing a breeze, allowing you to make almost any surface climbable. Need to climb a fence, a ladder, or perhaps a wall, no worries. This climbing mechanic can be easily created using a customized Interactable prefab that handles the climbing action in conjunction with the pseudo-body to handle the physics of the player’s body. In this chapter, we’ll make all Containers and the Ladder in our Demo scene climbable.

Climbing Mechanic Requirements

The VRTK provides you with two prefabs that are necessary to set up a climbing mechanic. To set up this climbing mechanic against game objects within your Demo scene, you’ll first require a Climbing Controller, whose responsibility is to communicate with the pseudo-body you already set up. You’ll also need a unique Climbable Interactable object that an Interactor can grab, which will serve as the foundation for your climbable object. Let’s begin by setting up the Climbing Controller.

Setting Up the Climbing Controller

To start, select the VRTK SETUP game object from within the hierarchy, and create an empty child game object and rename it “VRTK CLIMBING.”

Then, select and expand the “Packages” folder from within the Project tab. Locate the Tilia Locomotors Climbing Unity package and expand it until you reach its “Prefabs” folder. Drag and drop the Locomotors Climbing prefab available in the right-hand pane onto the VRTK CLIMBING game object in the hierarchy.

You now need to tell this Locomotors Climbing controller about the pseudo-body available in your Demo scene. This pseudo-body allows your Locomotors Climbing controller to apply world physics to your pseudo-body. With the Locomotors Climbing controller game object selected in the hierarchy, ensure that its Climbing Facade and Pseudo-Body Climb Target components have been expanded in the Inspector. Drag and drop the Trackers Pseudo-Body game object from the hierarchy into the Pseudo-Body Facade property parameter of the Pseudo-Body Climb Target component, as shown in Figure 13-1.
Figure 13-1

Setting up the Locomotors Climbing controller

Within the Demo scene, you’ll convert all Containers and the Ladder into climbable objects. To achieve this, the VRTK provides you with an Interactions Climbable prefab that an Interactor can grab. To make these objects climbable, you’ll need to add this Interactions Climbable prefab to each Container and the Ladder within the Demo scene.

Making the Containers Climbable

You’ll notice that the Demo scene has nine Containers in it, each of which has an appropriate Mesh and is fitted with a Box Collider. You’ll also notice that all these Containers have already been placed at strategic locations in the scene. You don’t want to change the position of these Container game objects when setting them up as climbable objects.

Let’s begin setting up the Containers to be climbable now. With the Tilia Locomotors Climbing Unity package “Prefabs” folder still selected within the Project tab, drag and drop the Interactions Climbable prefab into the hierarchy so that it is a child of your Demo scene. In the Scene tab, you’ll see that this Interactions Climbable prefab is simply a cube object with a box collider. You can verify this by selecting the Interactions Climbable game object in the hierarchy and navigating as follows: Interactions Climbable ➤ Internal ➤ Interactions Interactable ➤ Mesh Container ➤ Cube. This is depicted in Figure 13-2.
Figure 13-2

Navigating to the Cube child game object in the Interactions Climbable game object

To make a Container climbable, all you need to do is swap out the Cube game object shown in Figure 13-2 with your Container game object, which already contains a Mesh and box collider. For each Container you want to make climbable, you need to have a corresponding Interactions Climbable game object in the scene, so that its Cube game object can be swapped out with the concerned Container game object. For the Demo scene, this would mean you require nine Interactions Climbable game objects.

The procedure applied here is similar to the one you used when you swapped out your Interactable Cube for the Drill machine. Looking at Figure 13-2, you’ll see that the Interactions Climbable game object has an Interactions Interactable nested in it. This is the same Interactable that allowed you to grab the Drill machine.

You first need to set up a total of nine Interactions Climbable game objects in your scene, as you have nine Containers that need to make climbable. Select the Interactions Climbable game object in the hierarchy and duplicate it so that you have in total nine . From within the hierarchy, expand the Environment game object and then expand its Containers child game object, see Figure 13-3.
Figure 13-3

Hierarchy showing a total of nine created Interactions Climbable game objects

Next, select the first Container available in the Containers game object and ensure that the Transform component in the Inspector has been expanded. Copy these Transform settings by clicking the three vertical dots to the right of the Transform Component heading and then selecting the Copy Component menu item, as shown in Figure 13-4.
Figure 13-4

Copying the Transform settings for the first Container game object

Now, select the first Interactions Climbable game object in the hierarchy. Ensure that the Transform component in the Inspector has been expanded, and then expand the context menu for its Transform component by clicking the three vertical dots to the right of its Transform component heading. Then, select the Paste Component Values menu item, as shown in Figure 13-5.

This orients your Interactions Climbable white cube to the position of the Container whose transform values you copied. You’ll notice the white cube sitting in the Container.
Figure 13-5

Pasting the copied Transform setting values of the first Container game object into the first Interactions Climbable game object Transform

Now, expand this Interactions Climbable game object in the hierarchy until you reach its Cube game object, which is nested within the Mesh Container game object. This is depicted in Figure 13-2. Deactivate this Cube game object, as you want your Container game object to become the actual Mesh.

Next, drag the Container whose Transform values you copied from the “Containers” folder in the hierarchy and drop it onto the Mesh Container game object that is nested in the Interactions Climbable game object. This game object is currently expanded in the hierarchy, see Figure 13-6. Select this Container game object, which is now a child of the Mesh Container of the Interactions Climbable game object, and reset its Transform. Last, select this Interactions Climbable game object and rename it “Container 1.” You now need to set up the remaining eight Containers in the Demo scene using the same procedure as previously explained.
Figure 13-6

Dragging the first Container onto the Mesh Container nested in the Interactions Climbable game object and thereby making it a child replacing the original Cube game object

Once you’ve set up all nine Container objects to be climbable, select all of them in the hierarchy. Then, drag and drop them onto the Containers game object in the Environment game object. With all nine Containers still selected in the hierarchy, ensure that their Climbable Facade component has been expanded in the Inspector. Now, drag and drop the Locomotors Climbing game object from the hierarchy into the Climbing Facade property parameter. This connects each of your nine climbable Container game objects to the Locomotors Climbing controller, see Figure 13-7.

Now, you have all nine Containers grouped in the Containers game object, and they are all climbable.
Figure 13-7

Connecting the Locomotors Climbing controller to each of the climbable Containers

Making the Ladder Climbable

Let’s now set up your Ladder to be climbable. The procedure is similar to the one you used for making your Containers climbable. You’ll notice that your Ladder comprises two objects: Steel Ladder A and Steel Ladder B, each of them having its own Mesh and colliders. The Ladder game object is located in the hierarchy in the Environment game object. To set up this Ladder, you’ll need two Interactions Climbable prefabs, one for setting up Steel Ladder A to be climbable and the other for setting up Steel Ladder B similarly.

You should still have the “Prefabs” folder selected in your Tilia Locomotors Climbing Unity package. Drag and drop the Interactions Climbable prefab in the right-hand pane onto the Ladder game object in the hierarchy, and rename it “Interactions Climbable Steel Ladder A.” Then, duplicate this steel ladder and rename it “Interactions Climbable Steel Ladder B.”

Select both Interactions Climbable Steel Ladders in the hierarchy, and within the Inspector, ensure that their Climbable Facade components have been expanded. Drag and drop the Locomotors Climbing Controller game object from the hierarchy into the Climbing Facade property parameter.

Your Interactions Climbable Steel Ladder A and B game objects have now been made aware of the Locomotors Climbing controller, see Figure 13-8.
Figure 13-8

Connecting the Locomotors Climbing controller to the Interactions Climbable of Steel Ladder A and Steel Ladder B

Next, select the Steel Ladder A game object in the hierarchy. Copy its Transform settings by clicking the three vertical dots to the right of its Transform component heading and then selecting the Copy component menu item, as shown in Figure 13-9.
Figure 13-9

Copying the Transform settings for the Steel Ladder A game object

Now, select the Interactions Climbable Steel Ladder A game object in the hierarchy, and expand the context menu for its Transform component by clicking the three vertical dots to the right of its Transform Component heading. Then, select the Paste Component Values menu item, as shown in Figure 13-10.
Figure 13-10

Pasting the copied Transform setting values of the Steel Ladder A game object into the Interactions Climbable Steel Ladder A game object Transform

You have now embedded your Interactions Climbable Steel Ladder A, represented by a large white cube, in the Steel Ladder A game object whose transform values you copied. This is depicted in Figure 13-11.
Figure 13-11

The large white cube embedded in Steel Ladder A represents your Interactions Climbable Steel Ladder A game object

Now, from within the hierarchy, expand the Interactions Climbable Steel Ladder A game object until you reach its Cube object nested within the Mesh Container. Deactivate this Cube game object, as you want your Steel Ladder A game object to become the actual Mesh. Next, drag and drop the Steel Ladder A game object in the “Ladder” folder onto the Mesh Container game object nested within Interactions Climbable Steel Ladder A that is currently expanded in the hierarchy, see Figure 13-12.
Figure 13-12

Dragging Steel Ladder A onto the Mesh Container nested within the Interactions Climbable Steel Ladder A game object and thereby making it a child replacing the original Cube game object

Last, select the Steel Ladder A game object that is now a child of the Mesh Container and reset its Transform. We now need to set up Steel Ladder B using the same procedure we used for Steel Ladder A.

Deactivating Untouched Events on Climbable Game Objects

While climbing up any climbable object, you may find yourself falling for no apparent reason. This behavior may happen when you have your pseudo-body solving for body collisions during a climb. When pulling yourself up a Container, your pseudo-body will attempt to solve the body collision with the Container upon colliding with it. If you happen to let go of your grab at the same time, the Ungrab on Untouch Process attached to the Untouched event will be activated, as discussed later in the chapter, causing you to fall unexpectedly. There are a couple of ways to solve this issue. Let’s adopt the most straightforward approach.

Select and expand the Interactions Climbable Steel Ladder A game object in the hierarchy and locate its Interactions Interactable child game object and select it. In the Inspector, with the Interactable Facade component expanded, locate and expand the Untouched event in the “Touch Events” section. Set the drop-down value for this event, which currently is set to Runtime Only, to Off. This turns off the Untouched event, thereby not allowing it to execute whenever an Ungrab (Untouch) occurs while climbing. This ensures that the function attached to the Ungrab on Untouch Process in the event listener box is never called, guaranteeing that the unexpected falling behavior never occurs, see Figure 13-13.
Figure 13-13

Turning off the Untouched event, nested in the Interactions Interactable game object, for Interactions Climbable Steel Ladder A

Finally, you need to turn off this Untouched event for the Interactions Climbable Steel Ladder B game object, as well as for all nine climbable Container game objects in the Demo scene.

Now, playtest the climbable mechanic you’ve set up in the Demo scene. Climb up a couple of Containers as well as the Ladder, and you’ll notice that while climbing, you may encounter some jittering. Go ahead and try this out now.

To do away with this unwarranted jittering, you need to assign your Ladder and all the Containers to the Ignore Raycast layer. In the hierarchy, ensure that you have collapsed your Ladder and Containers game objects. Then, select these game objects in the hierarchy, and when prompted to set the Ignore Raycast layer for all child objects, select the button Yes, change children, as shown in Figure 13-14.
Figure 13-14

Setting the Layer for the Ladder and all Container game objects to Ignore Raycast

Go ahead and try out your climbing mechanic now to ensure you don’t experience any unexpected falling behavior or jittering while climbing.

You have now created an extremely sophisticated climbing mechanic and can easily climb any climbable object Tomb Raider style.

Summary

In this chapter, we learned how to set up climbing in VR using the VRTK’s provided prefabs. We started by setting up a Locomotors Climbing controller game object responsible for communicating with our pseudo-body during a climb. We then explored the Interactions Climbable prefab and learned how to set it up against each of the nine available Containers in the Demo scene. We next hooked up our Locomotors Climbing controller to each of our climbable Containers using the Climbing Facade property in the Climbable Facade component of our Climbable Container game objects. We used the same procedure to enable the Ladder to be climbable.

Finally, we went over a particular unexpected falling behavior that could occur while climbing on account of our pseudo-body attempting to solve for body collisions. To eliminate this possible falling behavior, we deactivated the Untouched event located within each climbable object’s Interactions Interactable game object. We also learned that by assigning our Ladder and Containers to the Ignore Raycast layer, we could eliminate any jittering that occurs while climbing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.160.131