简体   繁体   中英

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)

    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 . 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. 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.

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.

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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