如何让脚本知道GameObject被统一3D抓取

时间:2018-12-12 09:58:32

标签: c# unity3d virtual-reality steam

我正在使用SteamVR进行VR项目,我需要特定的事件(粒子系统)才能在控制器抓住GameObject时启动 我尝试使用此代码(C#)来确定是否抓住了该gameObject并使其无法正常工作(它看起来总是正确的)

    private Interactable interactable;
    public bool isGrabbed=false;

    // Use this for initialization
    void Start()
    {
        interactable = this.GetComponent<Interactable>();

    }

    // Update is called once per frame
    void Update()
    {
        if (interactable != null )
        {

            isGrabbed = true;
        }

    }

3 个答案:

答案 0 :(得分:0)

这里要检查的是组件interactable是否存在->检查它是否不存在null。当然,如果您不删除它,则它存在。

您可能应该使用的是提供的Throwable类或其他类。请查看SteamVR / InteractionSystem文件夹中的演示场景,因为它不久前已更新,现在功能更强大。

根据您的需要,您应该检查弓或任何您发现的东西。如果SteamVR文件夹名为“ SteamVR Unity Plugin-Input System.pdf”,其中包含文档,则还有一个文档pdf。

祝你好运!

答案 1 :(得分:0)

transform.GetComponent<OVRGrabbable>().isGrabbed

答案 2 :(得分:0)

您需要检查它是否附在手上:

if (interactable != null && interactable.attachedToHand != null) { ... }