SteamVRPlugin.2.0 抓取物体

实现功能侧握键抓取物体 双手可交换  再次按下侧握键放下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;
using Valve.VR;

[RequireComponent(typeof(Interactable))]
[RequireComponent(typeof(Rigidbody))]
public class PlayerGrab : MonoBehaviour
{
    [EnumFlags]
    [Tooltip("The flags used to attach this object to the hand.")]
    public Hand.AttachmentFlags attachmentFlags = Hand.AttachmentFlags.ParentToHand | Hand.AttachmentFlags.DetachFromOtherHand | Hand.AttachmentFlags.TurnOnKinematic;
    protected Interactable _interactable;
    [Tooltip("When detaching the object, should it return to its original parent?")]
    public bool restoreOriginalParent = false;
    private void Awake()
    {
        _interactable = this.GetComponent();
    }
    /// 
    /// 手进入物体中更新检测 
    /// 
    /// 
    public virtual void HandHoverUpdate(Hand hand)
    {
        Debug.Log("HandHoverUpdate");
        GrabTypes startingGrabType = hand.GetGrabStarting();
        if (startingGrabType != GrabTypes.None)
        {
            hand.AttachObject(gameObject, startingGrabType, attachmentFlags);
        }
    }
    /// 
    /// 手抓取物体回调
    /// 
    /// 
    protected virtual void OnAttachedToHand(Hand hand)
    {
        Debug.Log("OnAttachedToHand");
    }
    /// 
    /// 手抓取到物体更新检测
    /// 
    /// 
    protected virtual void HandAttachedUpdate(Hand hand)
    {
        GrabTypes startingGrabType = hand.GetGrabStarting();
        if (startingGrabType != GrabTypes.None)
        {
            hand.DetachObject(gameObject, restoreOriginalParent);
        }
        Debug.Log("HandAttachedUpdate");
    }
    /// 
    /// 手松开物体回调
    /// 
    /// 
    protected virtual void OnDetachedFromHand(Hand hand)
    {
        Debug.Log("OnDetachedFromHand");
    }
}

设置手柄抓取为侧握键

SteamVRPlugin.2.0 抓取物体_第1张图片

你可能感兴趣的:(C#,unity)