Teleport¶
Introduction¶
The Teleport is a common used feature in the VR environment and we will introduce how to implement the feature here.
Resources¶
There are foure prefabs used in the sample demonstrated below: WaveVR, ControllerLoader, InputModuleManager and WaveVRButtons.
All prefabs are located in Assets/WaveVR/Prefabs
Sample¶
After reading the document How to Change Head Location, you will know how to change the head location in the VR environment.
Assume that you have a sample scene, you will need to put the WaveVR, ControllerLoader and InputModuleManager prefabs under a parent object (e.g. TeleportRoot).
Drag the WaveVRButtons prefab into the sample scene for using the button events.
You will need to add a new script (e.g. Teleport) in the TeleportRoot object to set up the location.
Because of the origin of the head is (0, 0, 0), we assume the body height is 1.75m here in this sample.
In the code, get the head instance in the
Start
function:
// Use this for initialization
private GameObject head = null;
void Start () {
if (head == null && WaveVR_Render.Instance != null)
{
head = WaveVR_Render.Instance.gameObject;
}
}
- Get the teleport target location when receiving the button pressed event of the
Dominant Touchpad
key in theUpdate
function:
// Update is called once per frame
private Vector3 targetLocation = Vector3.zero;
void Update () {
if (WaveVR_Controller.Input (WaveVR_Controller.EDeviceType.Dominant).GetPressDown (wvr.WVR_InputId.WVR_InputId_Alias1_Touchpad))
{
targetLocation = WaveVR_RaycastResultProvider.Instance.GetRaycastResult (WaveVR_Controller.EDeviceType.Dominant).worldPosition;
}
}
- Then, write the teleport function
OnTeleport
:
private void OnTeleport(Vector3 position)
{
// Get the head's local position.
Vector3 head_position = head.transform.localPosition;
// Calculate the final teleport position.
Vector3 teleport_position = Vector3.zero;
teleport_position.x = position.x - head_position.x;
teleport_position.y = position.y + transform.localPosition.y; // Remember to add the body height.
teleport_position.z = position.z - head_position.z;
// Go teleport!
transform.localPosition = teleport_position;
}
- Finally, if you want to teleport when the
Dominant Touchpad
key is pressed, please call theOnTeleport
function inside theif
case of theUpdate
function:
void Update () {
if (WaveVR_Controller.Input (WaveVR_Controller.EDeviceType.Dominant).GetPressDown (wvr.WVR_InputId.WVR_InputId_Alias1_Touchpad))
{
targetLocation = WaveVR_RaycastResultProvider.Instance.GetRaycastResult (WaveVR_Controller.EDeviceType.Dominant).worldPosition;
OnTeleport (targetLocation);
}
}
Other Reference¶
If you are interested in more information about the Teleport feature, please refer to the sample Teleport_Test which is located in Assets/Samples/Teleport_Test.
The sample demonstrates another method of the Teleport. So there is not only one method to implement the Teleport feature and the main idea is to change the location of WaveVR’s parent object.