public void SetObjectSnapStatus(InteractableItemWithIsKinematic obj, bool status)
 {
     if (obj != null)
     {
         obj.SnapStatus(status);
     }
 }
    // Remove all items no longer colliding with to avoid further processing
    private void OnTriggerExit(Collider collider)
    {
        InteractableItemWithIsKinematic collidedItem = collider.GetComponent <InteractableItemWithIsKinematic>();

        if (collidedItem)
        {
            objectsHoveringOver.Remove(collidedItem);
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (controller == null)
        {
            Debug.Log("Controller not initialized");
            return;
        }
        //--------------Changes after 12.07.16----------------------
        #region ON_MENU_BUTTON

        /* if (controller.GetPressUp(menuButton))
         *     {
         *         if (!islaserController)
         *         {
         *             //enable the laser pointer to select UI buttons e-g group or ungroup
         *             this.gameObject.AddComponent<Wacki.ViveUILaserPointer>();
         *             islaserController = true;
         *         }else if(islaserController)
         *         {
         *             Destroy(this.gameObject.GetComponent<Wacki.ViveUILaserPointer>());
         *             islaserController = false;
         *             Destroy(this.transform.FindChild("Cube").gameObject);
         *             Destroy(this.transform.FindChild("Sphere").gameObject);
         *         }
         *      }*/
        #endregion

        #region TRANSLATE_ROTATE_ON_TRIGGER
        //For picking and dragging objects
        if (controller.GetPressDown(triggerButton))
        {
            // Find the closest item to the hand in case there are multiple and interact with it
            float minDistance = float.MaxValue;
            float distance;

            foreach (InteractableItemWithIsKinematic item in objectsHoveringOver)
            {
                distance = (item.transform.position - transform.position).sqrMagnitude;

                if (distance < minDistance)
                {
                    minDistance = distance;
                    closestItem = item;
                }
            }
            interactingItem = closestItem;
            closestItem     = null;

            if (interactingItem)
            {
                //set object name in UI
                if (this.transform.name.Equals("Controller (right)") && rightHandObject_InfoUI != null)
                {
                    rightHandObject_InfoUI.text = "Right-hand object: " + interactingItem.name;
                }
                if (this.transform.name.Equals("Controller (left)") && leftHandObject_InfoUI != null)
                {
                    leftHandObject_InfoUI.text = "Left-hand object: " + interactingItem.name;
                }

                if (interactingItem.IsInteracting())
                {
                    interactingItem.EndInteraction(this);
                }

                interactingItem.BeginInteraction(this);
                grabbedObject = true;
            }
        }

        //For dropping objects
        if (controller.GetPressUp(triggerButton) && interactingItem != null)
        {
            //check to snap if two gameobjects are in range
            interactingItem.EndInteraction(this);
            //set object name in UI
            if (this.transform.name.Equals("Controller (right)") && rightHandObject_InfoUI != null)
            {
                rightHandObject_InfoUI.text = "Right hand free";
            }
            if (this.transform.name.Equals("Controller (left)") && leftHandObject_InfoUI != null)
            {
                leftHandObject_InfoUI.text = "Left hand free";
            }
            grabbedObject = false;
        }
        #endregion

        //--------------Changes after 12.07.16----------------------
        #region SCALING_ON_GRIP
        controllerPosition = this.transform.position;
        if (controller.GetPressDown(gripButton))
        {
            gripButtonPressed = true;
        }
        else if (controller.GetPressUp(gripButton))
        {
            gripButtonPressed = false;
        }
        #endregion
    }
Example #4
0
    void Update()
    {
        #region INITIALIZATION
        if ((leftWand != null && leftWand.activeSelf) && (rightWand != null && rightWand.activeSelf))
        {
            if (wandController_Left == null)
            {
                wandController_Left = leftWand.GetComponent <WandControlForItems> ();
            }
            if (wandController_Right == null)
            {
                wandController_Right = rightWand.GetComponent <WandControlForItems> ();
            }
        }
        #endregion

        #region ACTIVE_WANDS_CONTROLLER
        if ((leftWand != null && leftWand.activeSelf) && (rightWand != null && rightWand.activeSelf))
        {
            #region SCALING
            //When  grip buttons are pressed together on controllers, scale up or down model
            if (wandController_Left.gripButtonPressed == true && wandController_Right.gripButtonPressed == true)
            {
                if (mainModelController != null)
                {
                    //group model here
                    _isScaling = true;
                    mainModelController.GroupModel();
                    wandController_Left.ChangePointersStatus(false);
                    wandController_Right.ChangePointersStatus(false);
                    mainModelController.ScaleModel(wandController_Left.controllerPosition, wandController_Right.controllerPosition);
                }
            }
            else
            {
                if (mainModelController._modelSplitted)
                {
                    mainModelController.UnGroupModel();
                }
                wandController_Left.ChangePointersStatus(true);
                wandController_Right.ChangePointersStatus(true);
                _isScaling = false;
            }
            #endregion

            #region ONE_HAND_UNSNAP
            //one handed interaction while two objects are already snapped, unsnap them???

            /*   if (wandController_Left.grabbedObject == true && wandController_Left.interactingItem.GetSnapStatus() == true)
             * {
             * //mainModelController.SetObjectSnapStatus(wandController_Left.interactingItem, false);
             * // mainModelController.SingleObjectHandle(wandController_Left.interactingItem.transform, "Left");
             * // mainModelController.EnableDisableIsKinematic(wandController_Left.interactingItem.transform, null, false);
             * //Debug.Log("for left hand isKinematic set to false");
             * }
             * if (wandController_Right.grabbedObject == true && wandController_Right.interactingItem.GetSnapStatus() == true)
             * {
             * //mainModelController.SetObjectSnapStatus(wandController_Right.interactingItem, false);
             * //mainModelController.SingleObjectHandle(wandController_Right.interactingItem.transform, "Right");
             * // mainModelController.EnableDisableIsKinematic(null, wandController_Right.interactingItem.transform, false);
             * // Debug.Log("for right hand isKinematic set to false");
             * }*/
            #endregion

            #region TWO_HAND_SNAP
            //When  both controllers hold some objects and those are dragged closely
            if (wandController_Left.grabbedObject == true && wandController_Right.grabbedObject == true)
            {
                #region CP1_NEW_SNAP
                if (wandController_Left.interactingItem.GetSnapStatus() == false || wandController_Right.interactingItem.GetSnapStatus() == false)
                {
                    // if the picked object is not snapped
                    if (mainModelController != null)
                    {
                        if (hasReadObjData == false)
                        {
                            mainModelController.SaveObjectsDataHandles(wandController_Left.interactingItem.transform, wandController_Right.interactingItem.transform);
                            hasReadObjData = true;
                        }
                        //  mainModelController.EnableDisableIsKinematic(wandController_Left.interactingItem.transform, wandController_Right.interactingItem.transform, true);
                        mainModelController.DrawABeamBetweenObjects(wandController_Left.interactingItem.transform, wandController_Right.interactingItem.transform);

                        _objectsAreInRange = mainModelController.CheckIFObjectsAreCloser(wandController_Left.interactingItem.transform, wandController_Right.interactingItem.transform);
                        if (_objectsAreInRange)
                        {
                            _refObjectLeft  = wandController_Left.interactingItem;
                            _refObjectRight = wandController_Right.interactingItem;
                            //Debug.Log("To snap: " + _refObjectLeft.transform.name + " " + _refObjectRight.transform.name);
                        }
                        else if (!_objectsAreInRange)
                        {
                            _refObjectLeft  = null;
                            _refObjectRight = null;
                        }
                    }
                }
                #endregion

                #region CP2_ALREADY_SNAPPED
                //check if initially any object is snapped and is child of a empty parent with tag "Snapped"
                //update position of parent transform instead of child object
                //but compare the new object with the first picked snapped object for relative distance and orientation?
                else if (wandController_Left.interactingItem.GetSnapStatus() == true || wandController_Right.interactingItem.GetSnapStatus() == true)
                {
                    if (wandController_Left.interactingItem.GetSnapStatus() == true && wandController_Left.interactingItem.transform.parent != null && wandController_Left.interactingItem.transform.parent.tag.Equals("Snapped"))
                    {
                        //save reference of the touched object but transform the parent
                        Debug.Log("Left object is already snapped");
                        _refObjectLeft = wandController_Left.interactingItem;
                    }
                    if (wandController_Right.interactingItem.GetSnapStatus() == true && wandController_Right.interactingItem.transform.parent != null && wandController_Right.interactingItem.transform.parent.tag.Equals("Snapped"))
                    {
                        //save reference of the touched object but transform the parent
                        Debug.Log("Right object is already snapped");
                        _refObjectRight = wandController_Right.interactingItem;
                    }
                    // Transform parent objects position and rotation
                    // ReferenceObject relative distance with the other object in wand which is not snapped yet.
                    // If all conditions are true then snap it too
                }
                #endregion
            }

            #region CP3_FREE_TO_SNAP
            else if (wandController_Left.grabbedObject == false && wandController_Right.grabbedObject == false)
            {
                //when both objects are set free to be snapped, execute only when 2 objects are in range
                if (_objectsAreInRange)
                {
                    if (_refObjectRight != null && _refObjectLeft != null)
                    {
                        Debug.Log("Snap objects; " + _refObjectLeft.transform.name + "  " + _refObjectRight.transform.name);
                        mainModelController.WhenCloserSnapObjects(_refObjectLeft.transform, _refObjectRight.transform);
                        mainModelController.SetObjectSnapStatus(_refObjectLeft, true);
                        mainModelController.SetObjectSnapStatus(_refObjectRight, true);
                        mainModelController.SetAsChildOfParent(_refObjectLeft.transform, _refObjectRight.transform);

                        _refObjectLeft  = null;
                        _refObjectRight = null;
                        hasReadObjData  = false;
                    }
                    _refObjectLeft     = null;
                    _refObjectRight    = null;
                    hasReadObjData     = false;
                    _objectsAreInRange = false;
                }
                else
                {
                    //there exist any line renderer on beam object delete it
                    if (mainModelController != null)
                    {
                        mainModelController.DeleteLineRendererFromBeamObjects();
                        hasReadObjData     = false;
                        _refObjectLeft     = null;
                        _refObjectRight    = null;
                        _objectsAreInRange = false;
                    }
                }
            }
            #endregion
            #endregion
        }
        #endregion
    }