New: VRTK Shu set of codes

Basic Configuration

Basic Configuration

 

1, handle key events acquisition

Note: obtaining an event not to use SteamVR_TrackedObject script. Because some blog say: the script on his right hand, to write another script to get it through the key events. But the script has a Bug, it causes rays shift out of position.

The right approach:

    //赋值右手
    public VRTK_ControllerEvents e;

    private void Start()
    {
        e.GripPressed += DoGripPressed;//抓取

        e.TouchpadAxisChanged += DoTouchpadAxisChanged;//touchpad事件
    }

    void DoGripPressed(object sender, ControllerInteractionEventArgs e)
    {

        print("按下了Trigger");
    }

    void DoTouchpadAxisChanged(object sender, ControllerInteractionEventArgs e)
    {
        //Debug.LogError(e.touchpadAngle);//获取touchpad角度值

        float jiaodu = e.touchpadAngle;
        //上  
        if (jiaodu < 45 && jiaodu > 0 || jiaodu > 315 && jiaodu < 360)
        {
            Debug.Log("上");
        }
        //右  
        if (jiaodu >= 45 && jiaodu < 135)
        {
            Debug.Log("右");
        }
        //下  
        if (jiaodu >= 135 && jiaodu < 225)
        {
            Debug.Log("下");
        }
        //左  
        if ((jiaodu >= 225 && jiaodu < 315))
        {
            Debug.Log("左");
        }
    }

 

2, handle shock

    private void Update()
    {
        //右手震动
        var deviceIndex2 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
        SteamVR_Controller.Input(deviceIndex2).TriggerHapticPulse(500);
    }

 

3, get-ray selected objects

    //赋值右手
    public VRTK_Pointer vRTK_Pointer;
 
    private void Start()
    {
        vRTK_Pointer.DestinationMarkerEnter += enter;
    }
 
    void enter(object sender,DestinationMarkerEventArgs e)
    {
        print(e.target.name);
    }

 

4, head-on collision to an object event

    //赋值PlayArea
    public VRTK_HeadsetCollision vRTK_HeadsetCollision;

    private void Start()
    {
        vRTK_HeadsetCollision.HeadsetCollisionDetect += VRTK_HeadsetCollision_HeadsetCollisionDetect; ;
    }

    private void VRTK_HeadsetCollision_HeadsetCollisionDetect(object sender, HeadsetCollisionEventArgs e)
    {
        //当头部碰到碰撞体后执行
        print(e.target.name);
    }

 

Published 320 original articles · won praise 77 · views 170 000 +

Guess you like

Origin blog.csdn.net/weixin_38239050/article/details/103336080