Ejemplo n.º 1
0
    public void DropObject()
    {
        // grab all sim objects that are currently colliding with magnet sphere
        foreach (KeyValuePair <SimObjPhysics, List <Collider> > sop in heldObjects)
        {
            Rigidbody rb = sop.Key.GetComponent <Rigidbody>();
            rb.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
            rb.isKinematic            = false;

            // delete cloned colliders
            foreach (Collider c in sop.Value)
            {
                Destroy(c.gameObject);
            }

            foreach (Collider c in sop.Key.MyColliders)
            {
                // re-enable colliders since they were disabled during pickup
                c.enabled = true;
            }

            if (sop.Key.IsOpenable)
            {
                CanOpen_Object coj = sop.Key.gameObject.GetComponent <CanOpen_Object>();
                coj.triggerEnabled = true;
            }

            GameObject topObject = GameObject.Find("Objects");

            if (topObject != null)
            {
                sop.Key.transform.parent = topObject.transform;
            }
            else
            {
                sop.Key.transform.parent = null;
            }

            rb.WakeUp();
        }

        // clear all now dropped objects
        heldObjects.Clear();
    }
Ejemplo n.º 2
0
    public bool PickupObject(List <string> objectIds, ref string errorMessage)
    {
        // var at = this.transform.InverseTransformPoint(armTarget.position) - new Vector3(0, 0, originToShoulderLength);
        // Debug.Log("Pickup " + at.magnitude);
        bool pickedUp = false;

        // grab all sim objects that are currently colliding with magnet sphere
        foreach (SimObjPhysics sop in WhatObjectsAreInsideMagnetSphereAsSOP())
        {
            if (objectIds != null)
            {
                if (!objectIds.Contains(sop.objectID))
                {
                    continue;
                }
            }

            Rigidbody rb = sop.GetComponent <Rigidbody>();
            rb.isKinematic = true;
            sop.transform.SetParent(magnetSphere.transform);
            rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
            if (sop.IsOpenable)
            {
                CanOpen_Object coj = sop.gameObject.GetComponent <CanOpen_Object>();

                // if an openable object receives OnTriggerEnter events
                // the RigidBody can be switched to Kinematic false
                coj.triggerEnabled = false;
            }

            // ok new plan, clone the "myColliders" of the sop and
            // then set them all to isTrigger = True
            // and parent them to the correct joint
            List <Collider> cols = new List <Collider>();

            foreach (Collider c in sop.MyColliders)
            {
                Collider clone = Instantiate(
                    c,
                    c.transform.position,
                    c.transform.rotation,
                    FourthJoint
                    );
                clone.isTrigger = true;

                // must disable the colliders on the held object so they
                // don't interact with anything
                c.enabled = false;
                cols.Add(clone);
            }

            pickedUp = true;
            heldObjects.Add(sop, cols);
        }

        if (!pickedUp)
        {
            errorMessage = (
                objectIds != null
                ? "No objects (specified by objectId) were valid to be picked up by the arm"
                : "No objects were valid to be picked up by the arm"
                );
        }

        // note: how to handle cases where object breaks if it is shoved into another object?
        // make them all unbreakable?
        return(pickedUp);
    }