Search code examples
c#unity-game-engineleap-motion

Cannot destroy object using leap motion fingers


Good day everyone . I have a little problem with detecting the collision of my fingers to my object . In my first stage my code for destroying object when it collides with my finger is here:

DetectionCollision.cs

using UnityEngine;
using System.Collections;
using Leap;
using Leap.Unity;

public class GetFlashLight : MonoBehaviour {

// Use this for initialization
void Start () {

}

private HandModel IsHand(Collider other){
    if (other.transform.parent && other.transform.parent.parent && other.transform.parent.parent.GetComponent<HandModel> ()) {
        return other.transform.parent.parent.parent.GetComponent<HandModel> ();
    } else {
        return null;
    }
}


void OnTriggerEnter(Collider other) {
    HandModel hand_model = IsHand (other);
    if (hand_model != null) {
        this.GetComponent<DisableObject> ().enabled = true;
    } else {
        this.GetComponent<DisableObject> ().enabled = false;
    }
}
}

DisableObject.cs

 public GameObject TurnOnFlashLight;
  // Use this for initialization
 void Start () {
    TurnOnFlashLight.gameObject.SetActive (true);
 }

The problem is when i apply the same code but different c# script (ofcourse) It didn't work . What do you think the problem is?

Components of my project


Solution

  • You can try the following to try find if the other collider has a hand:

    The 2nd version is put here if you'd prefer to fine tune the searching process or GetComponentInParent limitations are a stopper to you.

    using UnityEngine;
    
    internal class HandModel : MonoBehaviour
    {
    }
    
    internal class MyClass : MonoBehaviour
    {
        private HandModel TryGetHandVersion1(Collider other)
        {
            return other.GetComponentInParent<HandModel>();
        }
    
        private HandModel TryGetHandVersion2(Collider other)
        {
            var current = other.transform;
            while (current != null)
            {
                var handModel = current.GetComponent<HandModel>();
                if (handModel != null)
                    return handModel;
    
                current = current.parent;
            }
            return null;
        }
    
        private void OnTriggerEnter(Collider other)
        {
            var isHand = TryGetHandVersion1(other) != null;
        }
    }