Adding Levers | VR Unity Developer
With our gaze interactions set, we can finally get out of the escape room. Leaving this room brings event number four to a close and on to the next one. The final event in the VR pentathlon is event number five: demolition user interface. Let’s kick things off by adding some levers.
Start by moving the XR Rig over to the table by the excavator if you haven’t done so already. Next, there is an example XRIT example package to download, which will be needed for some assets that we will be using to control the excavator. In addition, like the VR demo scene covered earlier, there are prefabs and tools that you can use to explore and learn how they function within this particular scene.
Get Derek Anderson’s stories in your inbox
Join Medium for free to get updates from this writer.
One of these prefabs that we’re going to bring over to the VR pentathlon are levers. In the 3D UI folder of the XRIT example package, there are some prefabs that we can bring into the scene, including levers. Bring one in and place it on the table. Congratulations, you now have a lever in the scene.
Let’s make this more worthwhile and look at what the lever itself contains. In the Inspector View, there is a XR Lever script component that allows us to pick two events for the lever, On Lever Activate and On Lever Deactivate. In order for this lever to move the arm of the excavator, the excavator must be dragged into the event, with ExcavatorController.MoveArm taking care of the movement, which is currently moving and not moving.
Even though the excavator arm does move, in VR the default position seems backwards. The arm does move up when the lever stick is pushed up, but the default lever stick position is already pushed up with a X rotation value of 50. Unchecking the value option in the XR Lever script component and changing the value to -50 of the lever stick will set the default position of the lever stick to a more sensible position.
That takes care of moving the excavator arm up, but how about bringing it back down? We can repeat the process of adding and changing the values of the second lever in the same way as the first, with two differences. First, the lever stick will be facing away from the user, as the excavator arm will lower when pulled towards the user. Swapping the y-position of the lever 180 degrees will take care of that. Second, the float value for activating the lever will be -1, to indicate the downward motion.
You may need to modify the speed of the excavator arm when moving the levers in case it goes too fast.







