protected virtual void DropObject()
    {
        isGrabbing = false;

        if (!grabbedObject)
        {
            return;
        }

        grabbedObject.transform.parent = null;
        Rigidbody rb = grabbedObject.GetComponent <Rigidbody>();

        if (rb)
        {
            rb.isKinematic     = false;
            rb.velocity        = OVRInput.GetLocalControllerVelocity(thisController);
            rb.angularVelocity = OVRInput.GetLocalControllerAngularVelocity(thisController);
        }

        if (grabbedObjectScript)
        {
            grabbedObjectScript.myHolder = null;
            grabbedObjectScript          = null;
        }

        grabbedObject = null;
    }
示例#2
0
    public void DropObject()
    {
//		print("drop!");
        if (GrabbedObject.isSlotItem)
        {
            if (itemSlot && itemSlot.Empty)
            {
                GrabbedObject.slot = itemSlot.transform;
            }
            else if (aroundBag)
            {
                Transform emptySlot = inventorySystem.GetEmptySlot();
                GrabbedObject.slot = emptySlot;
                if (emptySlot != null)
                {
                    bag.GetComponent <AudioSource> ().Play();
                }
            }
            else
            {
                GrabbedObject.slot = null;
            }
        }
        if (GrabbedObject.DroppedBy(this))
        {
            GrabbedObject = null;
        }
    }
    protected virtual void GrabObject()
    {
        isGrabbing = true;

        RaycastHit[] hits = Physics.SphereCastAll(transform.position, grabRadius, transform.forward, 0f, grabMask);
        if (hits.Length > 0)
        {
            int closestHitIndex = 0;
            for (int i = 0; i < hits.Length; i++)
            {
                if (hits[i].distance > hits[closestHitIndex].distance)
                {
                    closestHitIndex = i;
                }
            }

            grabbedObject = hits[closestHitIndex].transform.gameObject;
            Rigidbody rb = grabbedObject.GetComponent <Rigidbody>();
            if (rb)
            {
                rb.isKinematic = true;
            }

            grabbedObjectScript = grabbedObject.GetComponent <GrabbedObject>();
            if (grabbedObjectScript)
            {
                grabbedObjectScript.myHolder = this;
            }
            grabbedObject.transform.parent = transform;
            if (centerGrabbedObject)
            {
                grabbedObject.transform.localPosition = Vector3.zero;
            }
        }
    }
示例#4
0
 private void OnTriggerExit2D(Collider2D collision)
 {
     if (collision.gameObject.GetComponent <GrabbedObject>() != null && objectInField != null)
     {
         objectInField = null;
     }
 }
示例#5
0
 /// <summary>
 /// Throw the current grabbed object.
 /// </summary>
 /// <param name="linearVelocity">Linear velocity of the throw.</param>
 /// <param name="angularVelocity">Angular velocity of the throw.</param>
 protected void ReleaseGrabbedObject(Vector3 linearVelocity, Vector3 angularVelocity)
 {
     _timeSinceLastRelease = Time.timeSinceLevelLoad;
     GrabbedObject.GrabEnd(this, linearVelocity, angularVelocity);
     OnGrabEnded?.Invoke(GrabbedObject?.gameObject);
     OnGrabTimedEnded?.Invoke(GrabbedObject?.gameObject, (_timeSinceLastRelease - _timeSinceLastGrab) ?? 0f);
     GrabbedObject = null;
 }
示例#6
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.GetComponent <GrabbedObject>() != null)
     {
         objectInField = collision.gameObject.GetComponent <GrabbedObject>();
     }
     Debug.Log(collision.gameObject.tag);
 }
示例#7
0
        /// <summary>
        /// Update the grabbed object position/rotation using the offset recorded when the grab started.
        /// </summary>
        /// <param name="pos">Current position of the grabber.</param>
        /// <param name="rot">Current rotation of the grabber.</param>
        protected virtual void MoveGrabbedObject(Vector3 pos, Quaternion rot)
        {
            if (GrabbedObject == null)
            {
                return;
            }
            Vector3    grabbablePosition = pos + rot * _grabbedObjectOffset.position;
            Quaternion grabbableRotation = rot * _grabbedObjectOffset.rotation;

            GrabbedObject.MoveTo(grabbablePosition, grabbableRotation);
        }
示例#8
0
    void DropObject()
    {
        if (grabObject != null)
        {
            // If the object has the GrabbedObject behavior, tell it to drop
            GrabbedObject grabbedObject = grabObject.GetComponent <GrabbedObject>();
            if (grabbedObject)
            {
                grabbedObject.dropObject(transform.parent);
            }

            grabObject = null;
        }
    }
示例#9
0
 public void Grab()
 {
     if (grabbedObject == null)
     {
         if (objectInField != null)
         {
             grabbedObject = objectInField;
             grabbedObject.OnGrab(transform);
         }
     }
     else
     {
         grabbedObject.OnRelease();
         grabbedObject = null;
     }
 }
示例#10
0
    void Update()
    {
        Debug.DrawRay(transform.parent.position, transform.parent.forward * 2f, Color.yellow);

        // If the wand button is released, drop the object
        if (getReal3D.Input.GetButtonUp(button))
        {
            DropObject();
        }
        // If the wand button was pressed and we're not already grabbing something, test for objects to grab
        else if (grabObject == null && getReal3D.Input.GetButtonDown(button))
        {
            // Raycast test for objects to grab
            RaycastHit hit     = new RaycastHit();
            bool       hitTest = Physics.Raycast(transform.parent.position, transform.parent.forward, out hit, 2.0f, grabLayerMask);
            if (hitTest)
            {
                Rigidbody rb = hit.rigidbody;
                Transform tf = hit.transform.parent;
                while (rb == null && tf.parent != null)
                {
                    tf = tf.parent;
                    rb = tf.GetComponent <Rigidbody>();
                }

                // If the object doesn't have a rigidbody, don't do anything
                if (!rb)
                {
                    return;
                }

                grabObject = rb.gameObject;

                // Add the GrabbedObject behavior(script) to the object if it hasn't already been grabbed by someone else
                GrabbedObject grabbedObject = grabObject.GetComponent <GrabbedObject>();
                if (!grabbedObject)
                {
                    grabbedObject = grabObject.AddComponent <GrabbedObject>();
                }

                // Grab the object
                grabbedObject.grabObject(transform.parent, allowGrabSteal);
            }
        }
    }
示例#11
0
    // Update is called once per frame

    private void OnCollisionEnter(Collision collision)
    {
        //Get the CharacterRagdoll
        //Get the GrabbableObject and check if its grabbable


        /*.gameObject.GetComponent<CharacterRagdoll>();*/

        IRagdoll ragdoll = collision.gameObject.GetComponent <IRagdoll>();

        if (ragdoll != null)
        {
            GrabbedObject grabbedObject = ragdoll.GetRagdoll().GetComponent <GrabbedObject>();

            if (grabbedObject != null)
            {
                if (grabbedObject.isAttachable) //not entering
                {
                    //Debug.Log("Attachable " + grabbedObject.isAttachable.ToString());
                    if (collision.gameObject.layer != 27)
                    {
                        //look for closest contact points
                        //  loop through all of the contacts points of the enemy
                        // find the point closest to the enemies position

                        //ContactPoint closestPoint;
                        //float closestPointDistance = float.MaxValue;
                        //foreach ( ContactPoint item in collision.contacts)
                        //{
                        //    float pointDistance = Vector3.Distance(item.point, ragdoll.GetRagdoll().hips.position);
                        //    if(pointDistance < closestPointDistance)
                        //    {
                        //        closestPoint = item;

                        //    }
                        //}
                        //AttachObject(ragdoll.GetRagdoll(), closestPoint.point, closestPoint.normal);
                        AttachObject(ragdoll.GetRagdoll(), ragdoll.GetRagdoll().hips.position, collision.contacts[0].normal);
                        grabbedObject.grabbable.isGrabable = false;
                    }
                }
            }
        }
    }
示例#12
0
 /// <summary>
 /// Throw the current grabbed object.
 /// </summary>
 /// <param name="linearVelocity">Linear velocity of the throw.</param>
 /// <param name="angularVelocity">Angular velocity of the throw.</param>
 protected void ReleaseGrabbedObject(Vector3 linearVelocity, Vector3 angularVelocity)
 {
     GrabbedObject.GrabEnd(this, linearVelocity, angularVelocity);
     OnGrabEnded?.Invoke(GrabbedObject?.gameObject);
     GrabbedObject = null;
 }