【SteamVR 2.0】7.制作 VR 抽屉 拉门 及扩展应用 LinearDrive
床头柜设计抽屉式扩展功能 #生活技巧# #居家实用技巧# #收纳小窍门#
使用线性控制的方式,结合SkeletonPoser,制作一些逼真的动作,例如手握抽屉拉开,手握把手开门等。
最终实现的效果
1.准备工作
首先我们需要一个抽屉模型,我去资源商店,下载了一个免费的抽屉模型
简单调整模型后,我选了一个抽屉,为其把手制作SkeletonPoser,并调整同步左右手(不了解SkeletonPoser和Interactable的基础使用,可以去我之前的第三节和第六节看看)
然后我们根据抽屉,轴的位置为其添加起始点A(InPos)和结束点B(OutPos)
这里我把抽屉单独出来,Drawer_Med1为原始的模型,我以此为基础做的SkeletonPoser
然后在其同级下,放置InPos和OutPos
我们需要抽屉,在InPos和OutPos间移动,并使用它们作为限制
类似如下思路
Vec3.Lerp,可以根据第三个参数,返回趋近于第一个参数或第二个参数的值
transform.position = Vector3.Lerp(InPos.position, OutPos.position, 一个0-1的动态值);
因此我们需要计算,当手抓握时,当前所在的位置,在AB点的相对位置百分比,并返回可以使用的0-1的值
注意,在计算投影时,AB向量需要归一化
代码实现如下
private float CalculateLocation01(Transform hand) {
Vector3 OriginVec3 = OutPos.position - InPos.position;
float length = OriginVec3.magnitude;
OriginVec3.Normalize();
Vector3 InPos2Hand = hand.position - InPos.position;
return Vector3.Dot(InPos2Hand, OriginVec3) / length;
}
最后,在HandAttachedUpdate中监听,并计算位置即可
protected virtual void HandAttachedUpdate(Hand hand) {
float currentLocation = CalculateLocation01(hand.transform);
transform.position = Vector3.Lerp(InPos.position, OutPos.position, currentLocation);
}
完整代码,挂载到制作SkeletonPoser的抽屉本体Drawer_Med1上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;
public class LinearDriveDrawer_test : MonoBehaviour
{
public Transform InPos;
public Transform OutPos;
private float CalculateLocation01(Transform hand) {
Vector3 OriginVec3 = OutPos.position - InPos.position;
float length = OriginVec3.magnitude;
OriginVec3.Normalize();
Vector3 InPos2Hand = hand.position - InPos.position;
return Vector3.Dot(InPos2Hand, OriginVec3) / length;
}
protected virtual void HandAttachedUpdate(Hand hand) {
float currentLocation = CalculateLocation01(hand.transform);
transform.position = Vector3.Lerp(InPos.position, OutPos.position, currentLocation);
}
}
3.应用扩展
基于这个方法,我们可以应用到任何线性的移动上,类似下面这种最基础的移动
让一个盒子,在AB两点移动
B点,可以随意改变位置。Cube都会在AB间移动
扩展:
既然我们获取到了AB间移动的0-1的值,那么就可以以此来控制动画的播放,进而以线性的手柄移动,来驱动更复杂的动画,以实现更好的效果。
例如我们实际上可以做手柄从A点移动到B点,但借助动画可以制作,看起来是拉开了一个复杂的机械装置,类似半条命这种效果
====20210723添加=====
上面说的,扩展到动画,这里做了一个小例子
首先,制作一个拉手的动画,
然后为拉环物体,制作SkeletonPoser,注意不要破坏动画的预设
然后,摆放AB点
接下来简单修改下脚本
主要使用currentLocation映射来控制Animator动画机,模拟拉环
anim.Play(clipInfo.clip.name, 0, currentLocation);
完整代码:脚本挂载拉环物体Object001上(名称测试用瞎起的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;
public class LinearDriveAnim_test : MonoBehaviour
{
public Transform InPos;
public Transform OutPos;
public Animator anim;
AnimatorClipInfo clipInfo;
private void Start() {
anim.speed = 0;
clipInfo = anim.GetCurrentAnimatorClipInfo(0)[0];
}
private float CalculateLocation01(Transform hand) {
Vector3 OriginVec3 = OutPos.position - InPos.position;
float length = OriginVec3.magnitude;
OriginVec3.Normalize();
Vector3 InPos2Hand = hand.position - InPos.position;
return Vector3.Dot(InPos2Hand, OriginVec3) / length;
}
protected virtual void HandAttachedUpdate(Hand hand) {
float currentLocation = CalculateLocation01(hand.transform);
anim.Play(clipInfo.clip.name, 0, currentLocation);
}
}
运行即可
网址:【SteamVR 2.0】7.制作 VR 抽屉 拉门 及扩展应用 LinearDrive https://www.yuejiaxmz.com/news/view/607901
相关内容
《产业结构调整》(Yanlz+VR云游戏+Unity+SteamVR+云技术+5G+AI+人工智能+虚拟现实+增强现实+语音语义图像识别+多传感器信息融合+智能人机交互系统+物联网+立钻哥哥+==)厨房拉篮实用还是抽屉实用
「抽屉拉篮」抽屉拉篮公司黄页
自制抽屉分隔 抽屉隔板有什么作用
抽屉组件的制作方法
明装暗拉手移门推拉门内嵌暗藏隐形衣柜抽屉拉手,批发价格:14.30
HIGOLD 悍高 希勒2.0厨房调料拉篮调味品工具收纳篮橱柜抽屉式多层置物架 673元
自动开关抽屉机构的制作方法
拉篮好还是抽屉好
拉篮和抽屉哪个实用 厨房抽屉拉篮怎么安装