Esempio n. 1
0
    // Finds the closest graspable object within range of the grasp.
    protected Collider FindClosestGrabbableObject(Vector3 graspPosition)
    {
        Collider closest          = null;
        float    minGraspDistance = this.GraspObjectDistance * this.GraspObjectDistance;

        Collider[] graspCandidates =
            Physics.OverlapSphere(graspPosition, GraspObjectDistance, GraspableLayers);
        for (int j = 0; j < graspCandidates.Length; ++j)
        {
            float squareDistance = (graspPosition - graspCandidates[j].transform.position).sqrMagnitude;

            if (graspCandidates[j].GetComponent <Rigidbody>() != null && squareDistance < minGraspDistance &&
                !graspCandidates[j].transform.IsChildOf(this.transform) &&
                graspCandidates[j].tag != "NotGrabbable")
            {
                GraspableObject grabbable = graspCandidates[j].GetComponent <GraspableObject>();
                if (grabbable == null || !grabbable.IsGrabbed())
                {
                    closest          = graspCandidates[j];
                    minGraspDistance = squareDistance;
                }
            }
        }

        return(closest);
    }
    private void OnTriggerEnter(Collider other)
    {
        if (ContainerController.Verbose)
        {
            UnityEngine.Debug.Log("collision with: " + other.name + "...");
        }
        // TODO: if the other gameobject has 1. a certain tag ("Target") and 2. has a GraspableObject script
        // attached to it and 3. is grasped, then the ObjectWasReleased event should be invoked, further,
        // the initiateDestruction method should be called, please set the validOrientation flag to
        // true in both cases

        if (ContainerController.Verbose)
        {
            UnityEngine.Debug.Log("collision with: " + other.gameObject.name + "...");
        }

        if (other.gameObject.tag == "Target")
        {
            GraspableObject graspable = other.gameObject.GetComponent <GraspableObject>();
            if (graspable != null)
            {
                if (graspable.IsGrabbed())
                {
                    bool validOrientation = true;

                    this.initiateDestruction(other.gameObject, validOrientation);
                    this.ObjectWasReleased(other.gameObject, validOrientation);
                }
                else
                {
                    if (ContainerController.Verbose)
                    {
                        UnityEngine.Debug.Log("target object was not grapsed: " + other.gameObject.name + "...");
                    }
                }
            }
            else
            {
                if (ContainerController.Verbose)
                {
                    UnityEngine.Debug.Log("target object is no graspable: " + other.gameObject.name + "...");
                }
            }
        }
    }