Example #1
0
        protected virtual void InitPrimaryUse(VRTK_InteractableObject currentUsedObject)
        {
            if (!currentUsedObject.IsValidInteractableController(gameObject, currentUsedObject.allowedGrabControllers))
            {
                usedObject = null;
                if (interactTongTouch != null && currentUsedObject.IsGrabbed(gameObject))
                {
                    interactTongTouch.ForceStopTouching();
                }
                return;
            }

            influencingUsedObject = false;
            currentUsedObject.SaveCurrentState();
            currentUsedObject.Grabbed(this);
            currentUsedObject.ZeroVelocity();
            currentUsedObject.isKinematic = false;
        }
    private IEnumerator ForceGrab()
    {
        yield return(null);

        if (touch && grab)
        {
            GameObject matchStick = Instantiate(Resources.Load <GameObject>("Prefabs/MatchStic"));
            touch.ForceStopTouching();
            grab.ForceRelease();
            touch.ForceTouch(matchStick);
            grab.AttemptGrab();
        }
    }
    // Update is called once per frame
    protected virtual void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            Debug.Log("space");
        }

        this.uniqueCenterZDelta = mrs.uniqueCenterZDelta;
        if (mrs.indirectTouch)
        {
            TouchNearestObject();
            indirectTouched = true;
        }
        else
        {
            if (indirectTouched)
            {
                VRTK_InteractTouch IT = this.transform.GetChild(1).GetComponent <VRTK_InteractTouch>();
                IT.ForceStopTouching();
                indirectTouched = false;
            }
        }


        swipeToRotate = mrs.swipeToRotate;
        //Debug.Log (mrs.smallMultiplesNumber);
        // rotate with the controller
        GameObject swipeTooltip    = this.transform.Find("SwipeToolTip").gameObject;
        GameObject rotationTooltip = this.transform.Find("RotationToolTip").gameObject;
        GameObject mainCamera      = GameObject.Find("Camera (eye)");
        float      facing          = mainCamera.transform.rotation.eulerAngles.y;

        if (gripped)
        {
            float lastZ     = z;
            float lastZBeta = zBeta;
            if (swipeToRotate)
            {
                z     = transform.localPosition.x;
                zBeta = transform.localPosition.z;
                swipeTooltip.SetActive(true);
            }
            else
            {
                z = transform.localRotation.eulerAngles.z;
                rotationTooltip.SetActive(true);
            }
            float diff     = z - lastZ;
            float diffBeta = zBeta - lastZBeta;
            for (int i = 1; i <= smallMultipleNumbers; i++)
            {
                GameObject building = GameObject.Find("Building " + i);
                //building.transform.RotateAround(Vector3.up, transform.rotation.y * Time.deltaTime);
                BuildingScript buildS     = building.transform.GetChild(0).gameObject.GetComponent <BuildingScript>();
                Vector3        realCentre = buildS.getCentreCoordinates();
                //building.transform.eulerAngles = new Vector3(0, transform.localRotation.eulerAngles.z, 0);
                if (swipeToRotate)
                {
                    float finalDiff = 0.0f;
                    if (Mathf.Abs(diff) > Mathf.Abs(diffBeta))
                    {
                        finalDiff = diff;
                    }
                    else
                    {
                        finalDiff = -diffBeta;
                    }
                    if (facing >= 0 && facing < 180)
                    {
                        building.transform.RotateAround(realCentre, building.transform.up, -finalDiff * 1000);
                    }
                    else
                    {
                        building.transform.RotateAround(realCentre, building.transform.up, finalDiff * 1000);
                    }
                }
                else
                {
                    building.transform.RotateAround(realCentre, building.transform.up, diff);
                }

                //building.transform.Rotate(diff * building.transform.up *  Time.deltaTime);
            }
        }
        else
        {
            if (swipeToRotate)
            {
                swipeTooltip.SetActive(false);
            }
            else
            {
                rotationTooltip.SetActive(false);
            }
        }

        if (padPressed)
        {
            if (this.name.Equals("Controller (right)"))
            {
                mrs.faceToCurve = true;
                mrs.ToggleFaceCurve();
            }
        }

        var system = OpenVR.System;

        if (system != null && system.GetControllerState(controllerIndex, ref controllerState, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t))))
        {
            ulong trigger = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Trigger));
            if (trigger > 0L && !triggerPressed)
            {
                triggerPressed = true;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnTriggerClicked(e);
            }
            else if (trigger == 0L && triggerPressed)
            {
                triggerPressed = false;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnTriggerUnclicked(e);
            }

            ulong grip = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_Grip));
            if (grip > 0L && !gripped)
            {
                gripped = true;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnGripped(e);
            }
            else if (grip == 0L && gripped)
            {
                gripped = false;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnUngripped(e);
            }

            ulong pad = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Touchpad));
            if (pad > 0L && !padPressed)
            {
                padPressed = true;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnPadClicked(e);
            }
            else if (pad == 0L && padPressed)
            {
                padPressed = false;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnPadUnclicked(e);
            }

            ulong menu = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_ApplicationMenu));
            if (menu > 0L && !menuPressed)
            {
                menuPressed = true;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnMenuClicked(e);
            }
            else if (menu == 0L && menuPressed)
            {
                menuPressed = false;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnMenuUnclicked(e);
            }

            pad = controllerState.ulButtonTouched & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Touchpad));
            if (pad > 0L && !padTouched)
            {
                padTouched = true;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnPadTouched(e);
            }
            else if (pad == 0L && padTouched)
            {
                padTouched = false;
                ClickedEventArgs e;
                e.controllerIndex = controllerIndex;
                e.flags           = (uint)controllerState.ulButtonPressed;
                e.padX            = controllerState.rAxis0.x;
                e.padY            = controllerState.rAxis0.y;
                OnPadUntouched(e);
            }
        }
    }
Example #4
0
    public override void Grabbed(GameObject grabbingObject)
    {
        VRTK_InteractGrab      grabScript        = grabbingObject.GetComponent <VRTK_InteractGrab>();
        VRTK_InteractTouch     touchScript       = grabbingObject.GetComponent <VRTK_InteractTouch>();
        VRTK_ControllerActions controllerActions = grabbingObject.GetComponent <VRTK_ControllerActions>();

        grabScript.ForceRelease();

        bool isRightController = grabbingObject.name.Contains("right");

        if (!alwaysUseSlider && (alwaysUseStatic || attachPtCount >= staticAttachPtCount))
        {
            //print("making static joint");
            staticAttachPtCount++;


            GameObject zbAttachPoint = Instantiate(StaticAttachPoint_PF);
            GameObject attachPoint   = zbAttachPoint.transform.GetChild(0).gameObject;

            foreach (ZB_PaddleBlade paddle in paddleBlades)
            {
                if (alwaysUseSlider)
                {
                    paddle.mainControlActions = controllerActions;
                }
                else
                {
                    paddle.secondaryControlActions = controllerActions;
                    paddle.ForcePoint = attachPoint.gameObject;
                }
            }

            ZB_AttachPoint attachPointScript = attachPoint.GetComponent <ZB_AttachPoint>();

            Transform spawnPoint = (grabScript && grabScript.controllerAttachPoint) ? grabScript.controllerAttachPoint.transform: grabbingObject.transform;
            //Transform spawnPoint = grabbingObject.transform.GetChild(0).transform.GetChild(1).GetChild(0).transform;

            if (isRightController)
            {
                rightSnapHandle = attachPoint.transform.GetChild(0).transform;
            }
            else
            {
                leftSnapHandle = attachPoint.transform.GetChild(0).transform;
            }

            attachPointScript.spawningObj = this;

            touchScript.ForceStopTouching();
            touchScript.ForceTouch(attachPoint.gameObject);

            zbAttachPoint.transform.position = spawnPoint.position;
            zbAttachPoint.transform.rotation = transform.rotation;
            float   attachPtLocalY = transform.InverseTransformPoint(zbAttachPoint.transform.position).y;
            Vector3 zeroPos        = transform.TransformPoint(new Vector3(0f, attachPtLocalY, 0f));
            zbAttachPoint.transform.position = zeroPos;

            ConfigurableJoint attachPtJoint = attachPoint.GetComponent <ConfigurableJoint>();

            //sliderJoint.connectedBody = rBody;

            attachPtJoint.connectedBody = rBody;
            SetStaticAttachPointJointRestraints(ref attachPtJoint);

            attachPtJoint.connectedBody = rBody;

            grabScript.AttemptGrab();

            if (isRightController)
            {
                rightControlLocalY = attachPtLocalY;
                currRightControl   = grabbingObject;
                if (alwaysUseSlider)
                {
                    rightRotateTrack.follow = true;
                }
                attachPointScript.isRightController = true;
                currRightAttachPt = attachPointScript.gameObject;
            }
            else
            {
                leftControlLocalY = attachPtLocalY;
                currLeftControl   = grabbingObject;
                if (alwaysUseSlider)
                {
                    leftRotateTrack.follow = true;
                }
                attachPointScript.isRightController = false;
                currLeftAttachPt = attachPointScript.gameObject;
            }
        }
        else
        {
            //print("making slider joint");
            attachPtCount++;

            foreach (ZB_PaddleBlade paddleBlade in paddleBlades)
            {
                paddleBlade.mainControlActions = controllerActions;
            }

            GameObject     zbAttachPoint     = Instantiate(AttachPoint_PF);
            GameObject     attachPoint       = zbAttachPoint.transform.GetChild(0).gameObject;
            GameObject     slider            = zbAttachPoint.transform.GetChild(1).gameObject;
            ZB_AttachPoint attachPointScript = attachPoint.GetComponent <ZB_AttachPoint>();

            Transform spawnPoint = (grabScript && grabScript.controllerAttachPoint) ? grabScript.controllerAttachPoint.transform : grabbingObject.transform;

            if (isRightController)
            {
                rightSnapHandle = attachPoint.transform.GetChild(0).transform;
            }
            else
            {
                leftSnapHandle = attachPoint.transform.GetChild(0).transform;
            }

            attachPointScript.spawningObj = this;

            touchScript.ForceStopTouching();
            touchScript.ForceTouch(attachPoint.gameObject);

            zbAttachPoint.transform.position = spawnPoint.position;
            zbAttachPoint.transform.rotation = transform.rotation;
            float   attachPtLocalY = transform.InverseTransformPoint(zbAttachPoint.transform.position).y;
            Vector3 zeroPos        = transform.TransformPoint(new Vector3(0f, attachPtLocalY, 0f));
            zbAttachPoint.transform.position = zeroPos;

            ConfigurableJoint sliderJoint   = slider.GetComponent <ConfigurableJoint>();
            ConfigurableJoint attachPtJoint = attachPoint.GetComponent <ConfigurableJoint>();

            attachPtJoint.connectedBody = rBody;
            SetStaticAttachPointJointRestraints(ref attachPtJoint);
            SetSliderJointRestraints(ref sliderJoint);

            attachPtJoint.connectedBody = rBody;
            sliderJoint.connectedBody   = rBody;

            grabScript.AttemptGrab();

            if (isRightController)
            {
                rightControlLocalY = attachPtLocalY;
                currRightControl   = grabbingObject;

                rightRotateTrack.follow = true;


                attachPointScript.isRightController = true;
                currRightAttachPt = attachPointScript.gameObject;
            }
            else
            {
                leftControlLocalY      = attachPtLocalY;
                currLeftControl        = grabbingObject;
                leftRotateTrack.follow = false;

                attachPointScript.isRightController = false;
                currLeftAttachPt = attachPointScript.gameObject;
            }

            StartCoroutine(DelayedJointConnect(attachPtJoint, slider.GetComponent <Rigidbody>()));
        }
    }