SteamVR学习(四)V1.2.3

双手柄控制

在抓取交互窗口设置中,Secondary Grab Attach有三种形式,Swap Controller为切换手柄,Control Direction为调整方向,Axis Scale可以控制比例缩放

使用Control Direction时,新建一个游戏物体作为物体旋转的轴向,把旋转的物体设置为他的子物体,把抓取设置放到这个新物体上,把Handle作为新物体的子物体,指定给Right Snap Handle,修改初始朝向时最好不要修改子物体的旋转角度等,要修改容器的旋转角度

使用Axis Scale时,可以设置锁定某个转向的缩放锁定,也可以设置等比例缩放

VRTK的UI交互

1.使用指针交互:

在需要交互的按钮上添加VRTK_UI Canvas脚本,在手柄上添加VRTK_UI Pointer脚本

2.使用手柄交互:

在手柄上添加VRTK_Interact Touch脚本

3.头部手柄配合交互:

在VRTK游戏物体下添加一个新游戏物体命名HeadSet,添加VRTK_Pointer和VRTK_Straight Pointer脚本表示从头部发出的视线,设置Tracer Visibility为Always Off,视线隐藏线段只显示一个点,添加VRTK_UI Pointer,然后指定一个引发交互事件的控制器,用其中一个手柄的按下作为事件的触发,在VRTK_Pointer和VRTK_UI Pointer的Controller中指定,添加VRTK_Transform Follow,设置跟随头部改变位置,Game Object To Follow设置为Camera(eye),Game Object To Change设置为HeadSet

使用指针实现拾取物体

给手柄添加VRTK_Interact Use,在VRTK_Pointer中勾选Interact With Objects,把物体设置为Is Usable,添加VRTK_Interact Use_Unity Events,然后编写程序实现功能

自定义手柄为手型并实现精确控制

1.给Left/Right Controller添加脚本时添加VRTK_Interact Use、VRTK_Interact Touch、VRTK_Interact Grab

2.导入手部模型,添加Mesh Colider组件,交互窗口设置可抓取可使用打开Animator

3.在Left/Right Controller中添加VRTK_Object Auto Grab脚本,配置Object To Grab为左右手模型,在手部模型上添加VRTK_InteractControllerAppearance,勾选Hide Controller On Grab

4.再改变手部模型与手柄的位置关系,添加Handler子物体,指定给手部模型中的Right/Left Snap Handler

5.新建脚本,把控制器指定进去

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;

public class Controller : MonoBehaviour
{
    public GameObject controller;//手柄控制器
    private Animator ani;//动画控制器
    // Start is called before the first frame update
    void Start()
    {
        //获取动画
        ani = GetComponent();
        //获取手柄按下和松开事件
        controller.GetComponent().UseButtonPressed += Controller_UseButtonPressed;
        controller.GetComponent().UseButtonReleased += Controller_UseButtonReleased;
        controller.GetComponent().TriggerAxisChanged += Controller_TriggerAxisChanged;
    }
    //变化过程中的处理函数
    private void Controller_TriggerAxisChanged(object sender, ControllerInteractionEventArgs e)
    {
        ani.Play("Grab", 0, e.buttonPressure);
        ani.speed = 0;
    }

    //Trigger松开处理函数
    private void Controller_UseButtonReleased(object sender, ControllerInteractionEventArgs e)
    {
        ani.SetTrigger("Release");
    }

    //Trigger按下处理函数
    private void Controller_UseButtonPressed(object sender, ControllerInteractionEventArgs e)
    {
        ani.SetTrigger("Grab");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

防穿墙

(黑屏)添加VRTK_Headset Collision Fade脚本

(其他)编写脚本挂载到PlayArea上,或者添加VRTK_Headset Collision_Unity Events脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;

public class Controller : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GetComponent().HeadsetCollisionDetect += Controller_HeadsetCollisionDetect;
    }

    private void Controller_HeadsetCollisionDetect(object sender, HeadsetCollisionEventArgs e)
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

自由落体

在PlayArea上添加VRTK_Body Physics脚本中的Grivaty Fall YThreshold

你可能感兴趣的:(学习,unity,vr)