绑定VR控制器的Trigger键和Grip键
- 在InputActions文件里面点击Edit asset。
- 创建新的Action命名为LeftGrip对应左手控制器的Grip键,设置其Action Type为Button(因为Grip是按钮类型)。然后将其绑定到XR Left Controller的gripButton上面(在XR Controller/XR Controller(Right Hand)/Optional Controls下面),右手控制器的Grip键同理。
- 创建新的Action命名为LeftTrigger对应左手控制器的Trigger键,设置其Action Type为Button。将其绑定到XR Left Controller的triggerButton上面(在XR Controller/XR Controller(Left Hand)/Optional Controls下面),右手控制器的Trigger键同理。
制作手的动画
- 创建文件夹Animations/Hand,然后创建Animator Controller并命名为AC_Hand(规则化命名)。
- 选中我们之前的WhiteLeftHand,并将AC_Hand关联到其上的Animator组件。
- 选中WhiteLeftHand,并点击Create按钮创建Animation。点击Animation窗口里红色的录制按钮,并在60帧的位置设置手各个手指关节的角度与图一致(也可自行调整),录制完成后点击播放可预览,确认完成后再点击录制按钮完成录制,得到一个手握拳的动画(此处命名为Grip),选中Grip动画片段,并不勾选Loop Time。
- 选中AC_Hand并在Animator视图中创建一个Layer命名为Grip,并将创建的Grip动画片段拖抓到视图中。
using UnityEngine;
using UnityEngine.InputSystem;
namespace VRMiniGames
{
public class PlayerHand : MonoBehaviour
{
[SerializeField] private InputActionReference _gripButton;
[SerializeField] private float _gestureSpeed = 5f;
[SerializeField] private Animator _animator;
private bool _enableHandGesture;
private float _currentGripLayerWeight;
private void Awake()
{
_gripButton.action.started += _ => { _enableHandGesture = true; };
_gripButton.action.canceled += _ => { _enableHandGesture = false; };
}
private void LateUpdate()
{
if (_enableHandGesture && _currentGripLayerWeight < 1)
{
SetLayerWeight(1f);
}
else if (!_enableHandGesture && _currentGripLayerWeight > 0)
{
SetLayerWeight(0f);
}
}
private void SetLayerWeight(float targetWeight)
{
_currentGripLayerWeight = Mathf.Lerp(_currentGripLayerWeight, targetWeight, Time.deltaTime * _gestureSpeed);
_animator.SetLayerWeight(1, _currentGripLayerWeight);
}
}
}
- 关联要输入的Grip键,引用的Animator以及手动画的插值速度。通过监听是否按下Grip键来决定目标动画Layer层级的权重值。按下则设目标动画层级权重值为1,松开则为0,变换过程为插值方式。
VR手模型按键对应触发手势