Understanding the Unity XR Toolkit Interaction System: Part 3
TheGreat! I’m finally showcasing my work instead of nerding out about the complexities of the XR Toolkit. So last week, I was re-working a cutting mechanic for Yi’s Eatery. Initially, we wanted to have a dynamic cutting system like this
Food objects would be cut like actual physical objects. However, in the end, we felt that there was a lot of complexity to implementing such a system correctly, even with tools like Ezy Slice. It also didn’t fit our game vision so instead, we opted for a different approach.
The idea was sort of like Overcooked, where players put items on the cutting board and chop it. There will be a chop progress bar shown, when the player chops it enough times, the chopped food will spawn in place of the original food.
However, here comes the problem and this was a persisting problem that was already made known to us when working on other mechanics. This problem was crippling and seriously damaged our progress in terms of VR mechanics (you can tell I really disliked it *pulls an extremely sad face* )
Essentially, the XR toolkit hates it if any interactable is destroyed while an interactor is interacting with it, meaning selecting, activation and....even hovering. Yes, if your interactor was HOVERING over an interactable while it was destroyed. The Interaction Manager would throw up errors and your interactor would be crippled.
However, I finally found a fix on Github after days of agony. Honestly, a huge thanks to the user who came up with this solution, I haven’t felt so happy about my work in a long time.
The fix is to edit the XRBaseInteractable script as one of its methods was the main cause of the errors.
Find this method in the XRBaseInteractable script and add on the line shown above. What this line does is that it prevents destroyed objects and its colliders from running this code before it is unregistered from the list of interactable colliders. This immediately stops the demonic errors that are thrown up everytime when objects are destroyed.
Soon after, I was able to get this end result relatively quickly.
As you can see, I can destroy the original cube and spawn a new prefab representing the chopped food in its place. I’ve also made sure that the structure of the code made it easy to add on UI elements, VFX, SFX etc. Anyways, yeah, that’s the end of my 3-parter devlog. I really like doing tutorials like these, it helps me consolidate my learning, and in turn, I hope it proves to be useful for other people some day!