Unity VR: Give the Player a Body, Sort Of…
In my last article, I got things moving by applying locomotion to the XR Origin rig. I could move within the scene, but I could also literally move right through anything in the scene. This short article focuses on fixing that issue. I’ll also look at how to adjust the height of the player object based on the height of the headset off the floor.
Adding Collision
In a standard 3D game with a player character game object, we’d add a collider and a Rigidbody to stop the player character from passing through objects in the scene. VR is a bit different. To handle the collisions, there is a component called Character Controller that we can add to the XR Origin game object. When applied, this adds a capsule collider to the object, similar to a capsule collider applied to an object in a standard 3D game.
This component does not make use of Rigidbody, so we won’t be adding that to the XR Origin.
Let’s take a quick look at some of the properties of the Character Controller.
Slope Limit — limits the slope that the object can climb. If the Slope Limit is 30, any slope greater than that, the game object (you) will not be able to climb.
Step Offset — The game object can step up a stair if it’s closer to the ground the the indicated value. The default is .3 meters. If the value is greater than the Character Controller’s height, and error will be generated.
Skin Width — Two colliders can penetrate each other as deep as their skin width. Setting this value to 10% of the Radius will reduce jitter and prevent the character from getting stuck.
Min Move Distance — this value indicates how far the character must move in order to realize the movement in the scene. This value is used to reduce jitter. In most cases this value should be at or near zero.
Center — used to offset the collider associated with the game object. This offset is applied to world space.
Radius — the length of the capsule collider’s radius. This is how wide the character object is in the scene.
Height — The capsule collider’s height. This value can be positive or negative, scaling it along the Y axis.
Height… we can set that value manually if we want to. But what if you’re short or really tall? Things could get a little wonky and not accurately represent you once you put the headset on. Fortunately, we can fix that with very little effort. Let’s take a look.
Adjusting the Height Based on the Headset Wearer’s Height
To allow the height of the XR Origin game object to automatically adjust to the height of the person wearing the headset, we can add a component to XR Origin called Character Controller Driver.
On this component, we can set the Min Height and the Max Height that the XR Origin can grow to, based on the height of the headset from the floor. It also requires a Locomotion Provider reference. There isn’t a Locomotion Provider on the XR Origin game object, but there is a Continuous Move Provider component. That component actually inherits from Locomotion Provider allowing us to use that component as the reference in this case.
With these two components now configured, we can now move about the scene, but we can no longer pass through the objects.