示例#1
0
    public override OVRCookedGrabbable GetGrabbable()
    {
        OVRCookedGrabbable copiedGrabbable = GameObject.Instantiate(grabbablePrefab, transform.position, Quaternion.identity);

        copiedGrabbable.GrabbedKinematic = grabbablePrefab.GetComponent <Rigidbody>().isKinematic;
        Debug.Log($"Created new copied grabbable. Kinematic: {copiedGrabbable.GrabbedKinematic}");
        return(copiedGrabbable);
    }
示例#2
0
 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.layer == LayerMask.NameToLayer("ingredient"))
     {
         OVRCookedGrabbable g = other.GetComponent <OVRCookedGrabbable>();
         if (g.isGrabbed)
         {
             Debug.Log($"Force release {g.name}");
             g.grabbedBy.ForceRelease(g, true);
         }
         AddIngredient(other.GetComponent <Ingredient>());
         Destroy(other.gameObject);
     }
 }
示例#3
0
    public void EmptyAndResetPlate()
    {
        // reset plate content
        foodObject.SetActive(false);
        content = null;
        ContentChanged?.Invoke(content);
        // if object is grabbed, force release before resetting, otherwise grab system will keep controlling its transform
        OVRCookedGrabbable g = GetComponent <OVRCookedGrabbable>();

        if (g.isGrabbed)
        {
            g.grabbedBy.ForceRelease(g);
        }

        // reset plate to original position
        GetComponent <ResettableTransform>().Reset();
    }
示例#4
0
    protected override void GrabBegin()
    {
        float closestMagSq = float.MaxValue;
        OVRCookedGrabbable closestGrabbable         = null;
        Collider           closestGrabbableCollider = null;

        // clean grab candidates in case it has missing references
        var itemsToRemove = m_grabCandidates.Where(f => f.Key == null).ToArray();

        foreach (var item in itemsToRemove)
        {
            m_grabCandidates.Remove(item.Key);
        }

        // Iterate grab candidates and find the closest grabbable candidate
        foreach (OVRGrabbable grabbable in m_grabCandidates.Keys)
        {
            if (grabbable == null)
            {
                continue;
            }
            bool canGrab = !(grabbable.isGrabbed && !grabbable.allowOffhandGrab);
            if (!canGrab)
            {
                continue;
            }

            for (int j = 0; j < grabbable.grabPoints.Length; ++j)
            {
                Collider grabbableCollider = grabbable.grabPoints[j];
                // Store the closest grabbable
                Vector3 closestPointOnBounds = Vector3.zero;
                closestPointOnBounds = grabbableCollider.ClosestPointOnBounds(m_gripTransform.position);

                float grabbableMagSq = (m_gripTransform.position - closestPointOnBounds).sqrMagnitude;
                if (grabbableMagSq < closestMagSq)
                {
                    closestMagSq             = grabbableMagSq;
                    closestGrabbable         = grabbable as OVRCookedGrabbable;
                    closestGrabbableCollider = grabbableCollider;
                }
            }
        }

        // Disable grab volumes to prevent overlaps
        GrabVolumeEnable(false);

        if (closestGrabbable != null)
        {
            if (closestGrabbable.isGrabbed)
            {
                (closestGrabbable.grabbedBy as OVRCookedGrabber).OffhandGrabbed(closestGrabbable);
            }

            m_grabbedObj          = closestGrabbable.GetGrabbable();
            m_grabbedOVRCookedObj = m_grabbedObj as OVRCookedGrabbable;

            // get closest grabbableCollider for this Grabbable
            // (we can get an instance of another grabbable because of OVRCopyGrabbable)
            closestMagSq = float.MaxValue;
            for (int j = 0; j < m_grabbedObj.grabPoints.Length; ++j)
            {
                Collider grabbableCollider = m_grabbedObj.grabPoints[j];
                // Store the closest grabbable
                Vector3 closestPointOnBounds = grabbableCollider.ClosestPointOnBounds(m_gripTransform.position);
                float   grabbableMagSq       = (m_gripTransform.position - closestPointOnBounds).sqrMagnitude;
                if (grabbableMagSq < closestMagSq)
                {
                    closestGrabbableCollider = grabbableCollider;
                }
            }
            m_grabbedObj.GrabBegin(this, closestGrabbableCollider);

            m_lastPos = transform.position;
            m_lastRot = transform.rotation;

            // Set up offsets for grabbed object desired position relative to hand.
            if (m_grabbedObj.snapPosition)
            {
                m_grabbedObjectPosOff = m_gripTransform.localPosition;
                if (m_grabbedObj.snapOffset)
                {
                    Vector3 snapOffset = m_grabbedObj.snapOffset.position;
                    if (m_controller == OVRInput.Controller.LTouch)
                    {
                        snapOffset.x = -snapOffset.x;
                    }
                    m_grabbedObjectPosOff += snapOffset;
                }
            }
            else
            {
                Vector3 relPos = m_grabbedObj.transform.position - transform.position;
                relPos = Quaternion.Inverse(transform.rotation) * relPos;
                m_grabbedObjectPosOff = relPos;
            }

            if (m_grabbedObj.snapOrientation)
            {
                m_grabbedObjectRotOff = m_gripTransform.localRotation;
                if (m_grabbedObj.snapOffset)
                {
                    m_grabbedObjectRotOff = m_grabbedObj.snapOffset.rotation * m_grabbedObjectRotOff;
                }
            }
            else
            {
                Quaternion relOri = Quaternion.Inverse(transform.rotation) * m_grabbedObj.transform.rotation;
                m_grabbedObjectRotOff = relOri;
            }

            // Note: force teleport on grab, to avoid high-speed travel to dest which hits a lot of other objects at high
            // speed and sends them flying. The grabbed object may still teleport inside of other objects, but fixing that
            // is beyond the scope of this demo.
            MoveGrabbedObject(m_lastPos, m_lastRot, true);
            SetPlayerIgnoreCollision(m_grabbedObj.gameObject, true);
            if (m_parentHeldObject)
            {
                m_grabbedObj.transform.parent = transform;
            }

            HapticsHelper.Vibration(
                this,
                m_controller,
                HapticsHelper.Duration.Short,
                HapticsHelper.Frequency.Medium,
                HapticsHelper.Amplitude.Low);

            if (m_grabbedOVRCookedObj.PlayPickUpSound)
            {
                audioSource.PlayOneShot(pickUpSound);
            }
        }
    }