这里的主要内容就是下述代码;
// 获取左手控制器的旋转(四元数表示)
Quaternion aRotationQuaternion = leftHandController.rotation;
// 计算旋转差值(四元数表示)
Quaternion rotationDifference = Quaternion.Euler(0, -90, -90);
// 应用差值到左手控制器的旋转并获取新的四元数
Quaternion bRotationQuaternion = aRotationQuaternion * rotationDifference;
// 将新的四元数设置给左手的旋转
leftHand.rotation = bRotationQuaternion;
下面是全部代码、加入Animation 的ik控制器和手部动画控制就可以实现手柄控制手臂手部位置移动、手部旋转和手指的操作了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
//头部控制器(vr头盔)
public Transform XROrigin;
//头部控制器(vr头盔)
public Transform headController;
//左手控制器(左手柄)
public Transform leftHandController;
//右手控制器(右手柄)
public Transform rightHandController;
public Transform player;
//头部rig
public Transform head;
//左手rig
public Transform leftHand;
//右手rig
public Transform rightHand;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//修改角色朝向
Vector3 cameraForward = headController.forward;
cameraForward.y = 0;
player.rotation = Quaternion.LookRotation(cameraForward);
//修改角色位置
Vector3 newPosition = XROrigin.position ;
transform.position = newPosition;
//修改角色左手位置
leftHand.position = leftHandController.position;
// 获取左手控制器的旋转(四元数表示)
Quaternion aRotationQuaternion = leftHandController.rotation;
// 计算旋转差值(四元数表示)
Quaternion rotationDifference = Quaternion.Euler(0, -90, -90);
// 应用差值到左手控制器的旋转并获取新的四元数
Quaternion bRotationQuaternion = aRotationQuaternion * rotationDifference;
// 将新的四元数设置给左手的旋转
leftHand.rotation = bRotationQuaternion;
}
}