// 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);
            }
        }
    }