示例#1
0
 private void OffhandGrabbed(Grabbable grabbable)
 {
     if (Grabbable == grabbable)
     {
         GrabbableRelease(Vector3.zero, Vector3.zero);
     }
 }
示例#2
0
        private void OnTriggerExit(Collider other)
        {
            Grabbable grabbable = other.GetComponentInParent <Grabbable>();

            if (grabbable == null)
            {
                return;
            }

            // Remove the grabbable
            int  refCount = 0;
            bool found    = GrabCandidates.TryGetValue(grabbable, out refCount);

            if (!found)
            {
                return;
            }

            if (refCount > 1)
            {
                GrabCandidates[grabbable] = refCount - 1;
            }
            else
            {
                GrabCandidates.Remove(grabbable);
            }
        }
示例#3
0
        public void ForceRelease(Grabbable grabbable)
        {
            bool canRelease = (
                (Grabbable != null) &&
                (Grabbable == grabbable)
                );

            if (canRelease)
            {
                GrabEnd();
            }
        }
示例#4
0
        private void GrabBegin()
        {
            float     closestMagSq             = float.MaxValue;
            Grabbable closestGrabbable         = null;
            Collider  closestGrabbableCollider = null;
            Vector3   closestGrabbableRotation = Vector3.zero;

            foreach (Grabbable grabbable in GrabCandidates.Keys)
            {
                for (int j = 0; j < grabbable.GrabPoints.Length; ++j)
                {
                    Collider grabbableCollider = grabbable.GrabPoints[j];
                    // Store the closest grabbable
                    Vector3 closestPointOnBounds = grabbableCollider.ClosestPointOnBounds(GrabPoint.position);
                    float   grabbableMagSq       = (GrabPoint.position - closestPointOnBounds).sqrMagnitude;
                    if (grabbableMagSq < closestMagSq)
                    {
                        closestMagSq             = grabbableMagSq;
                        closestGrabbable         = grabbable;
                        closestGrabbableCollider = grabbableCollider;
                        closestGrabbableRotation = grabbable.GrabRotations[j];
                    }
                }
            }

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

            if (closestGrabbable != null)
            {
                if (closestGrabbable.IsGrabbed)
                {
                    closestGrabbable.GrabbedBy.OffhandGrabbed(closestGrabbable);
                }

                Grabbable = closestGrabbable;
                Grabbable.SetLayer(GrabbableLayerInt);
                Grabbable.GrabBegin(this, closestGrabbableCollider, closestGrabbableRotation);

                // Set up offsets for grabbed object desired position relative to hand.
                GrabObjectPosOffset = -Grabbable.GrabbedCollider.transform.localPosition;
                GrabObjectRotOffset = Quaternion.Euler(Grabbable.GrabbedRotation);

                // TODO: 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.
                MoveGrabbedObject(GrabPoint.transform.position, GrabPoint.transform.rotation);

                RenderersEnable(false);
            }
        }
示例#5
0
        private void OnTriggerEnter(Collider other)
        {
            // Get the grab trigger
            Grabbable grabbable = other.GetComponentInParent <Grabbable>();

            if (grabbable == null)
            {
                return;
            }

            // Add the grabbable
            int refCount = 0;

            GrabCandidates.TryGetValue(grabbable, out refCount);
            GrabCandidates[grabbable] = refCount + 1;
        }
示例#6
0
 private void GrabbableRelease(Vector3 linearVelocity, Vector3 angularVelocity)
 {
     Grabbable.SetLayer(Grabbable.DefaultLayer);
     Grabbable.GrabEnd(linearVelocity, angularVelocity);
     Grabbable = null;
 }