コード例 #1
0
 private void OnDisable()
 {
     if (puppetHandTouching != null)
     {
         puppetHandTouching.touchedObject = null;
         puppetHandTouching = null;
         isTouched          = false;
         wasTouched         = false;
     }
 }
コード例 #2
0
        void EstablishObjectAsGrabbed(PuppetHand grabber)
        {
            // set the object state to grabbed
            isGrabbed = true;

            // tell the grabbed object which grabber is grabbing it
            puppetHandsGrabbing.Add(grabber);

            // clear the touched object
            grabber.touchedObject = null;

            // ignore collisions between object and characterController
            Physics.IgnoreCollision(GetComponent <Collider>(), PuppetJumpManager.Instance.puppetRig.characterController);

            // set the object to untouched
            Untouch();

            // invoke grabbed events for object
            grabEvents.grabbed.Invoke();
        }
コード例 #3
0
        public void SetAsGrabbed(PuppetHand grabber)
        {
            switch (grabStyle)
            {
            case GrabStyles.child:

                // if already being grabbed
                if (isGrabbed)
                {
                    // when child grabbed only one PuppetHand can grab at once
                    Release(puppetHandsGrabbing[0]);
                }

                StopAndPositiionForGrab();

                // parent the grabbedObject to the grabber
                transform.parent = grabber.transform;

                // if a rigidbody is present
                if (rigidbody != null)
                {
                    // add the mass to the grabber
                    grabber.rigidbody.mass += rigidbody.mass;

                    // remove the rigidbody from the grabbed object
                    Destroy(rigidbody);
                    rigidbody = null;
                }
                else
                {
                    // if the grabbed object is a connectable
                    // the rigidbody may have been removed during it's connection
                    if (GetComponent <Connectable>())
                    {
                        // if it had a rigidbody at the start
                        if (hasRigidBodyAtStart)
                        {
                            // add the mass to the grabber
                            grabber.rigidbody.mass += rbProperties.mass;
                        }
                    }
                }

                EstablishObjectAsGrabbed(grabber);

                // if the grab object is a connectable
                if (GetComponent <Connectable>())
                {
                    Connectable connectable = GetComponent <Connectable>();
                    HandleGrabbedConnectable(connectable);
                }
                break;

            case GrabStyles.fixedJoint:

                if (grabber.puppetString.grabJointFixed == null)
                {
                    StopAndPositiionForGrab();

                    // add a fixed joint to the grabber's string
                    grabber.puppetString.grabJointFixed = grabber.puppetString.gameObject.AddComponent <FixedJoint>();

                    // connect this object
                    grabber.puppetString.grabJointFixed.connectedBody = rigidbody;

                    // the amount of force it will take to break this joint
                    // for example when the object comes into contact with another object, like a table
                    grabber.puppetString.grabJointFixed.breakForce = grabBreakforce;

                    EstablishObjectAsGrabbed(grabber);

                    // if the grab object is a connectable
                    if (GetComponent <Connectable>())
                    {
                        Connectable connectable = GetComponent <Connectable>();
                        HandleGrabbedConnectable(connectable);
                    }
                }
                break;
            }
        }
コード例 #4
0
        public void Release(PuppetHand grabber)
        {
            switch (grabStyle)
            {
            case GrabStyles.child:
                // return to orginal parent
                transform.parent = originalParent;

                if (hasRigidBodyAtStart)
                {
                    // restore the rigidbody
                    rigidbody             = this.gameObject.AddComponent <Rigidbody>();
                    rigidbody.mass        = rbProperties.mass;
                    rigidbody.useGravity  = rbProperties.useGravity;
                    rigidbody.isKinematic = rbProperties.isKinematic;

                    // set the grabber back to it's orginal mass
                    grabber.rigidbody.mass = grabber.originalMass;
                }
                break;

            case GrabStyles.fixedJoint:
                if (grabber.puppetString.grabJointFixed != null)
                {
                    // destroy the joint between the grabber and the object
                    Destroy(grabber.puppetString.grabJointFixed);
                    grabber.puppetString.grabJointFixed = null;
                }
                break;
            }

            // THROW
            // check if object is throwable
            if (rigidbody != null && isThrowable)
            {
                rigidbody.AddForce(grabber.rigidbody.velocity, ForceMode.Impulse);
            }

            Connectable connectable = null;

            // if the grab object is a connectable
            if (GetComponent <Connectable>())
            {
                connectable = GetComponent <Connectable>();

                // if there is a Connectable Manager in the scene
                if (ConnectableManager.Instance)
                {
                    // if ghost are currently showing for this object
                    if (ConnectableManager.Instance.ghostShowingForThis == connectable)
                    {
                        ConnectableManager.Instance.ClearAllGhosts();
                    }
                }
            }

            // enable collisions between object and characterController
            Physics.IgnoreCollision(GetComponent <Collider>(), PuppetJumpManager.Instance.puppetRig.characterController, false);

            // remove grabber from list
            puppetHandsGrabbing.Remove(grabber);

            if (puppetHandsGrabbing.Count == 0)
            {
                // set the object state to not grabbed
                isGrabbed = false;
            }

            // if the grab object is a connectable
            if (connectable != null)
            {
                // check if a connection is made by letting go of this object
                int numConnectors = connectable.connectors.Count;
                for (int c = 0; c < numConnectors; c++)
                {
                    connectable.connectors[c].ConnectionCheck();
                }
            }

            if (puppetHandsGrabbing.Count == 0)
            {
                // invoke released events for object
                grabEvents.released.Invoke();
            }

            // empty the grabbed object
            grabber.grabbedObject = null;
        }
コード例 #5
0
        /// <summary>
        /// Makes a connection between two connectors.
        /// Sets the colliders of the connectables to ignor each other.
        /// May destroy the rigidbody of the newly connected connectable to keep only one rigidbody active in the hierarchy.
        /// Establishes the new heirarchy.
        /// </summary>
        void Connect()
        {
            // define the attached connectors
            attachedTo          = touching;
            touching.attachedTo = this.gameObject.GetComponent <Connector>();

            // cancel any collision between the connectables
            if (connectable.GetComponent <Collider>() && attachedTo.connectable.GetComponent <Collider>())
            {
                Physics.IgnoreCollision(connectable.GetComponent <Collider>(), attachedTo.connectable.GetComponent <Collider>());
            }

            if (connectable.GetComponent <Rigidbody>())
            {
                // kill all velocity as a result of connection
                connectable.GetComponent <Rigidbody>().velocity        = Vector3.zero;
                connectable.GetComponent <Rigidbody>().angularVelocity = Vector3.zero;

                if (destroyRigidBodyOnConnect)
                {
                    // remove the rigidbody of the connectable being added to the heirarchy in the next frame
                    Destroy(connectable.GetComponent <Rigidbody>());
                }
            }

            if (attachedTo.connectable.GetComponent <Rigidbody>())
            {
                // kill all velocity as a result of connection
                attachedTo.connectable.GetComponent <Rigidbody>().velocity        = Vector3.zero;
                attachedTo.connectable.GetComponent <Rigidbody>().angularVelocity = Vector3.zero;
            }

            // create the hierarchy
            connectable.transform.parent = attachedTo.connectable.transform;
            // position the child
            connectable.transform.localPosition    = attachedTo.transform.localPosition;
            connectable.transform.localEulerAngles = attachedTo.transform.localEulerAngles;

            if (connectable.GetComponent <Touchable>())
            {
                PuppetHand ph = connectable.GetComponent <Touchable>().puppetHandTouching;
                if (ph != null)
                {
                    ph.EndTouch();
                }
            }

            // indicate the connectors are connected to a compatible connector
            isConnected            = true;
            attachedTo.isConnected = true;

            //Debug.Log("Connection between " + connectable.gameObject.name + " and " + attachedTo.connectable.gameObject.name + " made.");

            // if there is a Connectable Manager in the scene
            if (ConnectableManager.Instance)
            {
                // adjust the masses of all connectables
                ConnectableManager.Instance.CheckMasses();
            }

            connectorEvents.connect.Invoke();
            attachedTo.connectorEvents.connect.Invoke();
        }