简体   繁体   English

如何让脚本知道在 unity 3d 中抓取了一个 GameObject

[英]How to let script know that a GameObject is grabbed in unity 3d

I am working on a VR project using SteamVR and I need specific event ( particle system ) to start when a GameObject is grabbed with the controllers I tried this code (C#) to determine if this gameObject is grabbed and it didn't work properly(it appears like it's always true)我正在使用 SteamVR 开发一个 VR 项目,当使用控制器抓取游戏对象时,我需要特定事件(粒子系统)启动它似乎总是如此)

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

    }
transform.GetComponent<OVRGrabbable>().isGrabbed

You need to check if it's attached to a hand:您需要检查它是否附在手上:

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

What you are checking here is if the Component interactable exist -> Checking if it is not null .您在这里检查的是 Component interactable是否存在 -> Checking if it is not null Of course, if you did not delete it, it exists.当然,如果你没有删除它,它是存在的。

What you should probably be using is the provided Throwable class or one of the others.您可能应该使用的是提供的Throwable类或其他类之一。 Have a look at the Demo Scene in the SteamVR/InteractionSystem Folder since it has been updated not too long ago and is now much more capable.查看 SteamVR/InteractionSystem 文件夹中的演示场景,因为它不久前更新过,现在功能更强大。

Depending on your needs you should check the Bow or whatever you find.根据您的需要,您应该检查弓或任何您找到的东西。 There is also a Documentation pdf if the SteamVR Folder called "SteamVR Unity Plugin - Input System.pdf" which includes Documentation.如果名为“SteamVR Unity Plugin - Input System.pdf”的 SteamVR 文件夹包含文档,则还有一个文档 pdf。

Good Luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM