【问题标题】:How to let script know that a GameObject is grabbed in unity 3d如何让脚本知道在统一 3d 中抓取了一个游戏对象
【发布时间】:2018-12-12 09:58:32
【问题描述】:

我正在使用 SteamVR 开发一个 VR 项目,当使用控制器抓取游戏对象时,我需要特定事件(粒子系统)来启动 我尝试了这段代码(C#)来确定这个游戏对象是否被抓取并且它不能正常工作(它看起来总是正确的)

    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;
        }

    }

【问题讨论】:

    标签: c# unity3d virtual-reality steam


    【解决方案1】:
    transform.GetComponent<OVRGrabbable>().isGrabbed
    

    【讨论】:

    • 请解释您的代码 sn-p 如何解决 OP 的问题。
    • 好吧...您只需将该代码 sn-p 插入 if,然后在 body 上启动派对系统...srlsy mate...您不会迷路 xD
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

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

      祝你好运!

      【讨论】:

      • 谢谢。我之前看过这个demo,从手榴弹里拿了这段代码不要在手中爆炸:if (interactable != null && interactable.attachedToHand != null) return;
      猜你喜欢
      • 2012-12-06
      • 1970-01-01
      • 2015-04-18
      • 2020-07-02
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多