private void DestroyPreview(VR_Grabbable grabbable)
 {
     if (previewOwner == grabbable.gameObject)
     {
         Destroy(preview);
     }
 }
Example #2
0
        private VR_Grabbable GrabbableRaycast()
        {
            RaycastHit[] hitArray        = Physics.SphereCastAll(GrabbableRay, grabRadius, grabDistance, layerMask, QueryTriggerInteraction.Ignore);
            float        minDistance     = Mathf.Infinity;
            VR_Grabbable closerGrabbable = null;
            RaycastHit   closerHitInfo   = new RaycastHit();

            for (int n = 0; n < hitArray.Length; n++)
            {
                float        d         = hitArray[n].distance;
                VR_Grabbable grabbable = VR_Manager.instance.GetGrabbableFromCollider(hitArray[n].collider);


                if (d < minDistance && IsValidGrabbableRaycast(hitArray[n]))
                {
                    minDistance     = d;
                    closerGrabbable = grabbable;
                    closerHitInfo   = hitArray[n];
                }
            }

            if (ShouldCheckForObstruction(closerGrabbable))
            {
                Collider col = GetFirstColliderIntersection(pointerTransform.position, (closerHitInfo.point - pointerTransform.position).normalized);
                if (col != closerHitInfo.collider)
                {
                    return(null);
                }
            }


            return(closerGrabbable);
        }
        public void OnGrabbableEnter(VR_Grabbable grabbable, bool force = false)
        {
            if (force)
            {
                ProcessDrop(grabbable);
            }


            if ((grabbable != null && grabbable.CurrentGrabState == GrabState.Grab) || force)
            {
                grabbable.AffectedDropZone = this;

                StartTracking(grabbable);


                if (!CanStackThisGrabbable(grabbable))
                {
                    return;
                }

                if (preview != null)
                {
                    Destroy(preview);
                }

                if (usePreview && droppedGrabbableList.Count == 0)
                {
                    CreatePreviewFor(grabbable);
                }
            }
        }
        private void OnGrabStateChange(GrabState grabState)
        {
            currentGrab.OnGrabStateChange.RemoveListener(OnGrabStateChange);
            currentGrab = Instantiate(grabbablePrefab, currentGrab.transform.position, currentGrab.transform.rotation);

            currentGrab.OnGrabStateChange.AddListener(OnGrabStateChange);
        }
        public void OnGrabbableExit(VR_Grabbable grabbable)
        {
            if (grabbable != null && droppedGrabbableList.Contains(grabbable))
            {
                Transform parent = grabbable.transform.parent;
                grabbable.transform.parent     = null;
                grabbable.transform.localScale = CalculateOriginalScale(grabbable);
                grabbable.transform.parent     = parent;
            }

            if (grabbable.AffectedDropZone == this)
            {
                grabbable.AffectedDropZone = null;
            }

            StopTrackingGrabbable(grabbable);

            if (previewOwner != null && previewOwner.gameObject == grabbable.gameObject)
            {
                DestroyPreview(grabbable);
                VR_Grabbable closerGrabbable = GetCloserGrabbable();

                if (closerGrabbable != null)
                {
                    CreatePreviewFor(closerGrabbable);
                }
            }
        }
Example #6
0
 private void OnDropStateChange(VR_Grabbable grabbable)
 {
     if (grabbable != null)
     {
         grabbable.transform.parent = parent;
     }
 }
        private bool ShouldUpdateAffectedDropZone(VR_Grabbable grabbable)
        {
            float distanceToThisDropZone  = (grabbable.transform.position - dropPoint.position).magnitude;
            float distanceToOtherDropzone = (grabbable.AffectedDropZone.DropPoint.position - grabbable.transform.position).magnitude;

            return(distanceToThisDropZone < distanceToOtherDropzone - 0.001f);
        }
        public void ApplyThrowVelocity(VR_Grabbable grabbable)
        {
            Rigidbody rb = grabbable.RB;

            //apply the hand velocity and angular velocity
            ProcessThrowVelocity(rb);
            ProcessThrowAngularVelocity(rb);
        }
        private bool CanDropGrabbable(VR_Grabbable grabbable)
        {
            if (grabbable == null)
            {
                return(false);
            }

            return(droppedGrabbableList.Count == 0 || CanStackThisGrabbable(grabbable));
        }
 private void StopTrackingGrabbable(VR_Grabbable grabbable)
 {
     if (grabbable != null && trackedGrabbable.Contains(grabbable))
     {
         trackedGrabbable.Remove(grabbable);
         grabbable.OnGrabStateChange.RemoveListener(onGrabStateChangeTrackConnections[grabbable]);
         onGrabStateChangeTrackConnections.Remove(grabbable);
     }
 }
Example #11
0
        private void ProcessInteraction(VR_Interactable interact)
        {
            interact.Interact(this);

            if (interact is VR_Grabbable)
            {
                currentGrab = interact as VR_Grabbable;
            }
        }
        private void OnColliderTriggerEnter(Collider other)
        {
            VR_Grabbable grabbable = VR_Manager.instance.GetGrabbableFromCollider(other);

            if (grabbable != null && !insideCollider.ContainsKey(other))
            {
                insideCollider.Add(other, grabbable);
            }
        }
        private void OnColliderTriggerExit(Collider other)
        {
            VR_Grabbable grabbable = VR_Manager.instance.GetGrabbableFromCollider(other);

            if (grabbable != null && insideCollider.ContainsKey(other))
            {
                insideCollider.Remove(other);
            }
        }
        private void SetColliderState(VR_Grabbable grabbble, bool state)
        {
            Collider col = grabbble.GetComponent <Collider>();

            if (col != null)
            {
                col.enabled = state;
            }
        }
        private void ProcessDrop(VR_Grabbable grabbable)
        {
            if (grabbable != null && !droppedGrabbableList.Contains(grabbable))
            {
                droppedGrabbableList.Add(grabbable);
                //grabbable.OnGrabStateChange.AddListener( delegate (GrabState state){ OnDroppedGrabbableGrabStateChange( grabbable, state ); } );

                UnityAction <GrabState> listener = delegate(GrabState state) { OnDroppedGrabbableGrabStateChange(grabbable, state); };
                onGrabStateChangeConnections[grabbable] = listener;
                grabbable.OnGrabStateChange.AddListener(listener);
            }
        }
        private void RemoveFromInsideCollider(VR_Grabbable grabbable)
        {
            List <Collider> allColliders = insideCollider.Keys.ToList();

            for (int n = 0; n < allColliders.Count; n++)
            {
                if (insideCollider[allColliders[n]] == grabbable)
                {
                    insideCollider.Remove(allColliders[n]);
                }
            }
        }
Example #17
0
        public void Explode()
        {
            Collider[] colliderArray = Physics.OverlapSphere(transform.position, explosionRange);

            Dictionary <DamageablePart, DamageableManager> connections = new Dictionary <DamageablePart, DamageableManager>();

            for (int n = 0; n < colliderArray.Length; n++)
            {
                DamageablePart damageable = colliderArray[n].GetComponent <DamageablePart>();

                if (damageable != null && !connections.ContainsValue(damageable.Owner))
                {
                    float distance       = Vector3.Distance(transform.position, damageable.transform.position);
                    float distanceFactor = Mathf.Abs((distance / explosionRange) - 1.0f);     // a distance factor from 0.0f to 1.0f
                    //set the connection
                    connections[damageable] = damageable.Owner;

                    //create damage info
                    DamageInfo info = new DamageInfo();
                    info.hitForce        = explosionForce;
                    info.hitPoint        = transform.position;
                    info.explosionRadius = explosionRange;
                    info.upwardsModifier = upwardsModifier;
                    info.dmg             = dmg * distanceFactor;
                    info.damageType      = DamageType.Explosion;

                    //send damage event
                    damageable.DoDamage(info);
                }
                else
                {
                    Rigidbody rb = colliderArray[n].GetComponent <Rigidbody>();

                    if (rb != null)
                    {
                        ApplyImpactForce(rb);
                    }
                    else
                    {
                        VR_Grabbable grabbable = VR_Manager.instance.GetGrabbableFromCollider(colliderArray[n]);

                        if (grabbable != null && grabbable.RB != null)
                        {
                            ApplyImpactForce(grabbable.RB);
                        }
                    }
                }
            }


            Destroy(gameObject, 5.0f);
        }
Example #18
0
        public VR_Grabbable GetGrabbableFromCollider(Collider c)
        {
            for (int n = 0; n < interactList.Count; n++)
            {
                VR_Grabbable grabbable = interactList[n] as VR_Grabbable;

                if (grabbable != null && grabbable.ColliderList != null && grabbable.ColliderList.Count > 0 && grabbable.ColliderList.Contains(c))
                {
                    return(grabbable);
                }
            }

            return(null);
        }
Example #19
0
        private void Awake()
        {
            rb = GetComponent <Rigidbody>();

            VR_Grabbable grabbable = GetComponent <VR_Grabbable>();

            if (grabbable == null)
            {
                Debug.LogError("Trowable needs VR_Grabbable script in order to work!");
                return;
            }

            GetComponent <VR_Grabbable>().OnGrabStateChange.AddListener(OnThisGrabbableStateChange);
        }
        private void CreatePreviewFor(VR_Grabbable grabbable)
        {
            previewOwner = grabbable.gameObject;
            DropZoneInfo dropZoneInfo = grabbable.GetComponent <DropZoneInfo>();

            preview = new GameObject(grabbable.gameObject.name + "_Preview");


            MeshFilter[]      meshFilters = grabbable.GetComponentsInChildren <MeshFilter>();
            CombineInstance[] combine     = new CombineInstance[meshFilters.Length];
            int i = 0;

            while (i < meshFilters.Length)
            {
                combine[i].mesh      = meshFilters[i].sharedMesh;
                combine[i].transform = grabbable.transform.worldToLocalMatrix * meshFilters[i].transform.localToWorldMatrix;

                i++;
            }

            MeshFilter filter = preview.AddComponent <MeshFilter>();

            filter.mesh = new Mesh();
            filter.mesh.CombineMeshes(combine);

            MeshRenderer renderer = preview.AddComponent <MeshRenderer>();

            renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            renderer.receiveShadows    = false;
            renderer.material          = transparentMat;
            renderer.material.color    = new Color(1.0f, 1.0f, 1.0f, 0.5f);


            preview.transform.position = CalculateDropEndPosition(grabbable);
            preview.transform.rotation = CalculateDropEndRotation(grabbable);

            if (dropZoneInfo != null)
            {
                Vector3 scale = dropZoneInfo == null ? grabbable.transform.localScale : dropZoneInfo.OriginalScale;
                scale *= dropZoneInfo.ScaleModifier;
                preview.transform.localScale = scale;
            }
            else
            {
                preview.transform.localScale = grabbable.transform.lossyScale;
            }

            preview.transform.parent = dropPoint;
        }
        private void OnThisDropStateChange(VR_Grabbable grabbable)
        {
            if (grabbable == null || !disableCollidersOnDrop)
            {
                return;
            }


            Collider[] collideraArray = grabbable.GetComponentsInChildren <Collider>();

            for (int n = 0; n < collideraArray.Length; n++)
            {
                collideraArray[n].enabled = false;
            }
        }
        private void HandleGrabbableEnterDropzone(VR_Grabbable grabbable)
        {
            //if the current grabbable is no being affected by any other dropzones juts call to grabbable enter
            if (grabbable.AffectedDropZone == null)
            {
                OnGrabbableEnter(grabbable);
            }

            //if the grabbable is being affected by any other dropzone
            else if (grabbable.AffectedDropZone != this && ShouldUpdateAffectedDropZone(grabbable))
            {
                grabbable.AffectedDropZone.OnGrabbableExit(grabbable);
                OnGrabbableEnter(grabbable);
            }
        }
Example #23
0
        //force a grab no distance check, and drop whathever you have on the hand
        public void ForceGrab(VR_Grabbable grabbable)
        {
            if (grabbable == null)
            {
                return;
            }

            if (currentGrab != null)
            {
                CleanCurrentGrab();
            }

            currentGrab = grabbable;
            currentGrab.OnGrabSuccess(this);
        }
        private bool IsInsideDropRange(VR_Grabbable grabbable)
        {
            bool insideRange = false;

            if (dropZoneMode == DropZoneMode.Distance)
            {
                float distance = (grabbable.transform.position - dropPoint.position).magnitude;
                insideRange = distance < dropRadius;
            }
            else
            {
                insideRange = insideCollider.ContainsValue(grabbable);
            }

            return(insideRange);
        }
Example #25
0
        private void Update()
        {
            if (controller.CurrentGrab != null)
            {
                if (activeGrabbable != null)
                {
                    activeGrabbable.RemoveActiveDistanceGrabController(controller);
                    SetActiveGrabbable(null);
                }

                return;
            }


            //get the closer intersection grabbable
            VR_Grabbable closerGrabbable = GrabbableRaycast();


            if (closerGrabbable == null)
            {
                if (activeGrabbable != null)
                {
                    activeGrabbable.RemoveActiveDistanceGrabController(controller);
                }

                SetActiveGrabbable(null);

                return;
            }
            if (closerGrabbable != null && activeGrabbable == null)
            {
                SetActiveGrabbable(closerGrabbable);
                activeGrabbable.AddActiveDistanceGrabController(controller);
            }
            else if (closerGrabbable != null && activeGrabbable != null && activeGrabbable != closerGrabbable)
            {
                activeGrabbable.RemoveActiveDistanceGrabController(controller);
                activeGrabbable = closerGrabbable;
                activeGrabbable.AddActiveDistanceGrabController(controller);
                controller.SetActiveDistanceGrabbable(activeGrabbable);
            }
            else if (closerGrabbable == null && activeGrabbable != null)
            {
                activeGrabbable.RemoveActiveDistanceGrabController(controller);
                SetActiveGrabbable(null);
            }
        }
        private VR_Grabbable GetCloserGrabbable()
        {
            float        minDistance     = float.MaxValue;
            VR_Grabbable closerGrabbable = null;

            for (int n = 0; n < trackedGrabbable.Count; n++)
            {
                float d = Vector3.Distance(trackedGrabbable[n].transform.position, dropPoint.position);

                if (d < minDistance)
                {
                    closerGrabbable = trackedGrabbable[n];
                    minDistance     = d;
                }
            }

            return(closerGrabbable);
        }
        private void StartTracking(VR_Grabbable grabbable)
        {
            if (!trackedGrabbable.Contains(grabbable))
            {
                trackedGrabbable.Add(grabbable);

                UnityAction <GrabState> unityAction = delegate(GrabState state)
                {
                    if (trackedGrabbable.Contains(grabbable))
                    {
                        OnGrabStateChange(state, grabbable);
                    }
                };

                grabbable.OnGrabStateChange.AddListener(unityAction);

                onGrabStateChangeTrackConnections[grabbable] = unityAction;
            }
        }
Example #28
0
        public void SetActiveDistanceGrabbable(VR_Grabbable grabbable)
        {
            activeDistanceGrabbable = grabbable;

            if (activeDistanceHighlight != null)
            {
                activeDistanceHighlight.UnHighlight(this);
            }


            if (grabbable != null)
            {
                activeDistanceHighlight = grabbable.GetComponent <VR_Highlight>();
            }
            else
            {
                activeDistanceHighlight = null;
            }
        }
        /// <summary>
        /// Called when the current dropped grabbable change his grab state
        /// </summary>
        /// <param name="state"></param>
        private void OnDroppedGrabbableGrabStateChange(VR_Grabbable grabbable, GrabState state)
        {
            if (state == GrabState.Grab)
            {
                onUndrop.Invoke(grabbable);

                //remove the listener
                grabbable.OnGrabStateChange.RemoveListener(onGrabStateChangeConnections[grabbable]);
                OnGrabbableExit(grabbable);

                droppedGrabbableList.Remove(grabbable);
                onGrabStateChangeConnections.Remove(grabbable);

                if (dropZoneMode == DropZoneMode.Collider)
                {
                    RemoveFromInsideCollider(grabbable);
                }
            }
        }
        private IEnumerator FlyRoutine(VR_Grabbable grabbable)
        {
            float elapseTime = 0.0f;

            Vector3    startPosition = grabbable.transform.position;
            Quaternion startRotation = grabbable.transform.rotation;

            while (elapseTime < FlyTime)
            {
                elapseTime += Time.deltaTime;
                float lerp = elapseTime / flyTime;


                if (syncronizePosition)
                {
                    grabbable.transform.position = Vector3.Lerp(startPosition, CalculateDropEndPosition(grabbable), lerp);
                }
                if (syncronizeRot)
                {
                    grabbable.transform.rotation = Quaternion.Lerp(startRotation, CalculateDropEndRotation(grabbable), lerp);
                }

                yield return(new WaitForSeconds(Time.deltaTime));
            }

            onDrop.Invoke(grabbable);
            grabbable.enabled = true;

            if (syncronizePosition)
            {
                grabbable.transform.position = CalculateDropEndPosition(grabbable);
            }
            if (syncronizeRot)
            {
                grabbable.transform.rotation = CalculateDropEndRotation(grabbable);
            }

            if (preview != null)
            {
                preview.gameObject.SetActive(false);
            }
        }