This tutorial will guide you through setting up hands in VR. This tutorial uses the Oculus headset. In addition you must have the windows OS running and a PC strong enough to handle VR
Note: project file name: XRInteractableTwo
We will continue using Unity's action based XR interaction toolkit. When using the XR interaction toolkit, there aren't any hands so you will need to create your own
VRHands.unitypackage by going to Assets > Import Package > Custom PackageHierarchy window > XR Rig > Camera Offset > LeftHand Controller and then in the Inspector window > Model >model prefab and from the Assets folder click on > VR Hands > Prefabs(which you imported in step #1) and drag and drop the Left Hand into the model prefab slotHierarchy window > XR Rig > Camera Offset > RightHand Controller and then in the Inspector window > Model >model prefab and from the Assets folder click on > VR Hands > Prefabs(which you imported in step #1) and drag and drop the Right Hand into the model prefab slot
Now lets write a script. In the Scripts folder > Right click > Create > C# Script > Rename it HandController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class HandController : MonoBehaviour
{
[SerializeField] private InputActionAsset actionAsset;
[SerializeField] private string controllerName;
[SerializeField] private string actionNameTrigger;
[SerializeField] private string actionNameGrip;
public InputActionReference actionReference;
private InputActionMap _actionMap;
private InputAction _inputActionTrigger;
private InputAction _inputActionGrip;
private Animator _handAnimator;
// Start is called before the first frame update
void Awake()
{
//get all of our actions...
_actionMap = actionAsset.FindActionMap(controllerName);
_inputActionGrip = _actionMap.FindAction(actionNameGrip);
_inputActionTrigger = _actionMap.FindAction(actionNameTrigger);
//get the Animator
_handAnimator = GetComponent<Animator>();
}
private void OnEnable()
{
_inputActionGrip.Enable();
_inputActionTrigger.Enable();
}
private void OnDisable()
{
_inputActionGrip.Disable();
_inputActionTrigger.Disable();
}
// Update is called once per frame
void Update()
{
var gripValue = _inputActionGrip.ReadValue<float>();
var triggerValue = _inputActionTrigger.ReadValue<float>();
_handAnimator.SetFloat("Grip", gripValue);
_handAnimator.SetFloat("Trigger", triggerValue);
}
}
Assets folder click on > VR Hands > Prefabs and click on the Left Hand prefab and drag and drop the HandController.cs script into the Inspector window for the Left Hand
Change the values for the Action Asset, Controller Name, Action Name Trigger, Action Name Grip properties as shown above
In the Hierarchy window > XR Rig > Camera Offset > LeftHand Controller go to the Inspector window and uncheck the Line Renderer and XR Interactor Line Visual boxes
Also under the XR Ray Interactor change Hit Detection Type to Sphere cast and change the Sphere Cast Radius to 0.013
In the Hierarchy window > XR Rig > Camera Offset > RightHand Controller go to the Inspector window and uncheck the Line Renderer and XR Interactor Line Visual boxes
Also under the XR Ray Interactor change Hit Detection Type to Sphere cast and change the Sphere Cast Radius to 0.013