Make a fully functional calculator in Unity not only for VR, Part III
Similarly, we can assign the BoxCollider of the CalcButton to the Poke Collider property of the XR Poke Filter component and for each XR Poke Follow Affordance component, we can assign the transform component of the corresponding CalcButton to the Poke Follow Transform property. xr. xr. xr. hands": "1. 3. 3. 3. 3.
In the previous part, we finished our calculator and gained knowledge about tokenization, a recursive descent parser, and the Abstract Syntax Tree (AST). Today, our focus will be on adapting this project to a virtual reality VR environment, using the OpenXR, XR Interaction Toolkit, and XR Hands packages.
These packages are provided by Unity Technologies. They provide a level of abstraction that allows us to worry less about the differences between VR devices.
However, note that I have tested all of this only on Valve Index. While the steps should theoretically be independent of the device, there may be minor differences that could arise in practice.
In the event of any troubles, I recommend referring to the documentation of the abovementioned packages. You can find the respective links provided in the first paragraph.
Exporting Calc Prefab
If you have been following along with the previous parts and would like to continue doing so in this section, export the Calc.prefab we've created. Right-click on the Project content tab and from the context menu that appears, select Export Package... .
,
"com.unity.xr.interaction.toolkit": "2.3.2",
"com.unity.xr.openxr": "1.7.0",Though, you need to know the exact name of the package and the version number you'd like to add. Also, note that these packages evolve, and what applies in this tutorial for these specific versions might be very different in future versions.
Now, go to Edit → Project Settings and find XR Plug-in Management. Within this settings, enable OpenXR from Plug-in Providers.
PressVRHand()
{
if (isPressed)
return;
isPressed = <span class="hljs-literal">true</span>;
OnPressed?.Invoke(token);
}
public void ReleaseVRHand() { isPressed = false; }
Now, unpack the Calc prefab completely by right-clicking on it in the hierarchy and selecting Prefab → Unpack Completely. This will allow us to wire things up differently, eventually resulting in a new prefab.
Next, nest each object its name starts with CalcButton into a new empty game object with a name that starts with PokeButton as shown in the following image.
Awake()
{
XRSimpleInteractable interactable = GetComponentInParent<XRSimpleInteractable>();
interactable.hoverEntered.AddListener((args) => { PressVRHand(); });
interactable.hoverExited.AddListener((args) => { ReleaseVRHand(); });
XRPokeFilter filter = GetComponentInParent<XRPokeFilter>();
filter.pokeCollider = GetComponent<BoxCollider>();
XRPokeFollowAffordance affordance = GetComponentInParent<XRPokeFollowAffordance>();
affordance.pokeFollowTransform = transform;
}
This will save us from a lot of manual dragging and dropping in the Unity Editor, and it is also less prone to errors.
For the remaining values that are the same for all the mentioned components on the PokeButtons, you can select them all and set the following in the Inspector:
Poke Directionproperty ofXR Poke FiltertoY.Max Distanceproperty ofXR Poke Follow Affordanceto0.25.
follow me on Twitter, where you can find not only more of my tutorials, but also other interesting things related to game development.