本文主要是介绍pico手柄和人物模型手部旋转同步,实现手柄控制手臂手部位置移动、手部旋转和手指的操作了,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这里的主要内容就是下述代码;
// 获取左手控制器的旋转(四元数表示)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;//头部rigpublic Transform head;//左手rigpublic Transform leftHand;//右手rigpublic Transform rightHand;//设置默认的模型身高public float playerHeight = 2.0f;// Start is called before the first frame updatevoid Start(){/*初始化身高*/float playerzoom = headController.position.y / playerHeight;Debug.Log(playerzoom);player.localScale = new Vector3(playerzoom, playerzoom, playerzoom);}// Update is called once per framevoid Update(){//修改角色朝向和启动转向动画(我这里没有启动动画)Vector3 cameraForward = headController.forward;cameraForward.y = 0;player.rotation = Quaternion.LookRotation(cameraForward);//修改角色位置 Vector3 newPosition = XROrigin.position ;transform.position = newPosition;//头部旋转Quaternion headControllerRotationQuaternion = headController.rotation; head.rotation = headControllerRotationQuaternion;//根据头盔高度修改模型蹲的数值、修改动画(我这里没有写)//左手操作 // 计算旋转差值(四元数表示)Quaternion rotationDifference = Quaternion.Euler(0, -90, -90); HandRotation(leftHandController, leftHand, rotationDifference);//右手操作// 计算旋转差值(四元数表示)Quaternion rightrotationDifference = Quaternion.Euler(0, 90, 90); HandRotation(rightHandController, rightHand, rightrotationDifference);}void HandRotation(Transform HandController, Transform Hand, Quaternion rotationDifference ) {//修改角色手位置Hand.position = HandController.position;// 获取手控制器的旋转(四元数表示)Quaternion aRotationQuaternion = HandController.rotation;// 应用差值到手控制器的旋转并获取新的四元数Quaternion bRotationQuaternion = aRotationQuaternion * rotationDifference;// 将新的四元数设置给左手的旋转Hand.rotation = bRotationQuaternion;}}
这篇关于pico手柄和人物模型手部旋转同步,实现手柄控制手臂手部位置移动、手部旋转和手指的操作了的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!