Getting Started with XR Toolkit in Unity (Part 4) — Locomotion
In the last section, we setup our XR Rig and its inputs. Today we’ll give our player a way to move. Let’s get to it!
Locomotion Setup
To get started in moving our character around our scene we need to add the Locomotion script provided with XR Toolkit to our XR Rig. Click on the XR Rig in the Hierarchy, then in the Inspector, go to “Add Component,” search for “Locomotion System,” and add it to the XR Rig. Next, drag our XR Rig gameobject into the “XR Rig” slot so that the Locomotion System has a reference to our XR Rig script. The locomotion component script should now look like this:
Next we want to add the “Continuous Move Provider (Action-based)” script. The references for the Left Hand and Right Hand Move Actions should be populated however we don’t need the Right Hand Move Action. To disable it, just uncheck the “Use Reference” for the “Right Hand Move Actions.” The reason for this is that we’ll be using only the left hand controller to move.
Next change “Gravity Application Mode” to “Immediately” so we won’t have any weird issues.
Now all we need to do is drag in our XR Rig into the “System” field and our Main Camera into the “Forward Source” field. Our completed Continuous Move Provider component should now look like this:
Great!
Now character can move… err… almost. We need a way to turn.
Turning
The XR Toolkit provides two turning methods: Continuous and Snap. Continuous allows you to change direction in one fluid motion, same as you would in a first-person shooter. This is great method of turning however, for some people, this will cause dizziness and nausea after using this for a while. If you are one of those people (Like me!) then using the Snap Turn method will be the better option. The snap method will turn the player at discrete angles making it easier for the brain to process rotation in the virtual world without its proprioceptive counterpart.
Since I can’t use the Continuous turning method, I’m going to add the script for snap turning. Click “Add Component” in our XR Rig’s Inspector window and search for “Snap Turn Provider (Action Based).”
Like with the Continuous Move Provider, the references for “Left Hand Snap Turn Action” and “Right Hand Snap Turn Action” should be populated. Disable the Left Hand Snap Turn Action since we are using the left hand to only move. Next, drag in the XR Rig game object into the “System” field to add our Locomotion System.
Finally, our XR Rig needs a Character Controller. Click “Add Component” again and search for “Character Controller.” Once added you are all set!
You are now ready to use your VR Player in your scene!