Пример #1
0
    // Update is called once per frame
    void Update()
    {
        Transform [] controllerTransforms = experimentManager.GetControllerTransforms();
        SteamVR_Controller.Device [] controllerDevices = experimentManager.GetControllers();
        rayCastLineUi.enabled = false;

        // Determine which hand is being used for laser
        if (laserActive == false)
        {
            for (int i = 0; i < 2; i++)
            {
                if (controllerDevices[i].GetPress(EVRButtonId.k_EButton_Grip))
                {
                    laserActive      = true;
                    activeLaserIndex = i;
                    break;
                }
            }
        }

        // Determine if laser is still active
        bool currentLaserStatus = controllerDevices[activeLaserIndex].GetPress(EVRButtonId.k_EButton_Grip);

        if ((currentLaserStatus == false) && (laserActive == true) && (lastHit.point != Vector3.zero) && canMoveThere)
        {
            experimentManager.TeleportUser(lastHit.point);
        }
        laserActive           = currentLaserStatus;
        rayCastLineUi.enabled = laserActive;

        // Draw out line if laser is active
        if (laserActive == true)
        {
            rayCastLineUi.enabled = true;
            rayCastLineUi.SetPosition(0, controllerTransforms[activeLaserIndex].position + controllerTransforms[activeLaserIndex].forward.normalized * 0.025f);
            rayCastLineUi.startColor = Color.red;
            rayCastLineUi.endColor   = Color.red + Color.white * 0.75f;

            RaycastHit hit;
            Physics.Raycast(controllerTransforms[activeLaserIndex].position,
                            controllerTransforms[activeLaserIndex].TransformDirection(Vector3.forward), out hit, Mathf.Infinity);
            lastHit = hit;

            // Great user is pointing at something, see if we can move there
            canMoveThere = false;
            if (hit.point != Vector3.zero)
            {
                rayCastLineUi.SetPosition(1, hit.point);

                if (hit.collider.name.Length >= moveableEnding.Length)
                {
                    if (hit.collider.name.Substring(hit.collider.name.Length - moveableEnding.Length, moveableEnding.Length) == moveableEnding)
                    {
                        canMoveThere             = true;
                        rayCastLineUi.startColor = Color.green;
                        rayCastLineUi.endColor   = Color.green + Color.white * 0.75f;
                    }
                }
            }

            // User is not pointing at anything, we can't move there....
            else
            {
                rayCastLineUi.SetPosition(1, controllerTransforms[activeLaserIndex].position +
                                          controllerTransforms[activeLaserIndex].forward.normalized * 3.0f);
            }
        }
    }