Using VRTK in Unity to Implement Controller Raycasting and Roaming Functions#
@[TOC](Table of Contents)
By studying the new examples provided by VRTK: 【002-Pointers StraightPointer】, 【003-Pointers BezierPointers】, and 【004-Locomotion Teleporting】, I have achieved the functionalities I needed in the early stages.
Description of the 【002-Pointers StraightPointer】 Example#
In this scene, a straight line appears when you touch the touchpad. Pressing and releasing the touchpad will select the object that the ray touches, and you can see the object's border color change. At the same time, the Unity console will print the name of the selected object, the distance between the controller and the object, and the position of the ray's endpoint on the object.
Description of the 【003-Pointers BezierPointers】 Example#
In this scene, when you touch the touchpad, the controller emits a curve. Pressing and releasing the touchpad will select. You can see three option tiles in the scene; selecting the left tile will change the ray to linear, while selecting the right tile will change the ray style to a custom style. In this scene, the custom style changes the ray's endpoint to a halo after it contacts the object. Selecting the middle tile will set the style to the default Bezier curve style.
Description of the 【004-Locomotion Teleporting】 Example#
In this scene, pressing the touchpad emits a ray, and releasing it allows you to teleport to the position of the pointer cursor. You can teleport a cube to a rock surrounded by a mesh collider, with no height restrictions. Additionally, a script is used to restrict the gray cube so that it cannot be a teleportation point.
You can experience and learn on your own.
For convenience in later interactions, my personal development habit is: the left controller emits a curve for teleportation, while the right controller emits a straight line for interaction. I have already set up the basic environment for VR development; if you don't understand, you can refer to my previous articles. Below is the development implementation for emitting rays and teleportation.
- Add an empty object named 【PlayArea】 under 【VRTK_Scripts】 and add the VRTK component 【VRTK_DashTeleport】 (I personally prefer this, as there are many scripts for teleportation; learn to use it on your own):
- Add the components 【VRTK_ControllerEvents】 (script for listening to controller events), 【VRTK_StraightPointerRenderer】 (script for emitting straight lines, which can change ray styles and colors), and 【VRTK_Pointer】 (for rendering ray pointers) to the controller object, as shown below:
- Similarly, add 【VRTK_ControllerEvents】, 【VRTK_BezierPointerRenderer】 (this is the script for emitting curves), and 【VRTK_Pointer】 to the left controller:
Here, to make the pointer cursor look better, you can set it as shown in the image below:
You can also learn to create your favorite styles. At this point, the basic configuration is complete.
Function Testing#
The controller emits rays for roaming; here, I am using an emulator. Press and hold the 【Q】 key on the keyboard to emit rays, and release to teleport:
Left controller emitting rays for roaming:
The basic functionality has been implemented; I will improve it later.