Example #1
0
        public void GrabGrabbable(Grabbable grab)
        {
            // Grab is already in Snap Zone
            if (grab.transform.parent != null && grab.transform.parent.GetComponent <SnapZone>() != null)
            {
                return;
            }

            if (HeldItem != null)
            {
                ReleaseAll();
            }

            HeldItem      = grab;
            heldItemRigid = HeldItem.GetComponent <Rigidbody>();

            if (HeldItem.CompareTag("Ball"))
            {
                HeldItem.GetComponent <Ball>().isInHolster = false;
            }


            // Mark as kinematic so it doesn't fall down
            if (heldItemRigid)
            {
                heldItemWasKinematic      = heldItemRigid.isKinematic;
                heldItemRigid.isKinematic = true;
            }
            else
            {
                heldItemWasKinematic = false;
            }

            // Set the parent of the object
            grab.transform.parent = transform;

            // Set scale factor
            // Use SnapZoneScale if specified
            if (grab.GetComponent <SnapZoneScale>())
            {
                _scaleTo = grab.GetComponent <SnapZoneScale>().Scale;
            }
            else
            {
                _scaleTo = ScaleItem;
            }

            // Is there an offset to apply?
            SnapZoneOffset off = grab.GetComponent <SnapZoneOffset>();

            if (off)
            {
                offset = off;
            }
            else
            {
                offset = grab.gameObject.AddComponent <SnapZoneOffset>();
                offset.LocalPositionOffset = Vector3.zero;
                offset.LocalRotationOffset = Vector3.zero;
            }

            // Lock into place
            if (offset)
            {
                HeldItem.transform.localPosition    = offset.LocalPositionOffset;
                HeldItem.transform.localEulerAngles = offset.LocalRotationOffset;
            }
            else
            {
                HeldItem.transform.localPosition    = Vector3.zero;
                HeldItem.transform.localEulerAngles = Vector3.zero;
            }

            // Disable the grabbable. This is picked up through a Grab Action
            disableGrabbable(grab);

            // Call Grabbable Event from SnapZone
            if (OnSnapEvent != null)
            {
                OnSnapEvent.Invoke(grab);
            }

            // Fire Off Events on Grabbable
            GrabbableEvents[] ge = grab.GetComponents <GrabbableEvents>();
            if (ge != null)
            {
                for (int x = 0; x < ge.Length; x++)
                {
                    ge[x].OnSnapZoneEnter();
                }
            }

            if (SoundOnSnap)
            {
                VRUtils.Instance.PlaySpatialClipAt(SoundOnSnap, transform.position, 0.75f);
            }

            LastSnapTime = Time.time;
        }