Exemplo n.º 1
0
    /// <summary>
    ///     Once per frame
    /// </summary>
    public void Update()
    {
        Vector3 a = mainCamera.WorldToScreenPoint(transform.position);
        Vector3 b = new Vector3(a.x, a.y + 40, a.z);

        Vector3 aa = mainCamera.ScreenToWorldPoint(a);
        Vector3 bb = mainCamera.ScreenToWorldPoint(b);

        transform.localScale = Vector3.one * (aa - bb).magnitude;

        for (int i = 0; i < 3; i++)
        {
            if (Input.GetMouseButton(0) && detectors[i].pressing)
            {
                isInteracting = true;

                mT.OnMove();

                // Get the distance from the camera to the target (used as a scaling factor in translate)
                float distance = Vector3.Distance(Camera.main.transform.position, translateTarget.transform.position);
                distance = distance * 2.0f;

                // Will store translate values
                Vector3 offset = Vector3.zero;

                switch (i)
                {
                // X Axis
                case 0:
                {
                    // If the user is pressing the plane, move along Y and Z, else move along X

                    if (detectors[i].pressingPlane)
                    {
                        float deltaY = Input.GetAxis("Mouse Y") * (Time.deltaTime * distance);
                        offset = Vector3.up * deltaY;
                        offset = new Vector3(0.0f, offset.y, 0.0f);
                        translateTarget.transform.Translate(offset);

                        float deltaZ = Input.GetAxis("Mouse X") * (Time.deltaTime * distance);
                        offset = Vector3.forward * deltaZ;
                        offset = new Vector3(0.0f, 0.0f, -offset.z);
                        translateTarget.transform.Translate(offset);
                    }
                    else
                    {
                        float delta = Input.GetAxis("Mouse X") * (Time.deltaTime * distance);
                        offset = Vector3.left * delta;
                        offset = new Vector3(-offset.x, 0.0f, 0.0f);
                        translateTarget.transform.Translate(offset);
                    }
                }
                break;

                // Y Axis
                case 1:
                {
                    // If the user is pressing the plane, move along X and Z, else just move along X

                    if (detectors[i].pressingPlane)
                    {
                        float deltaX = Input.GetAxis("Mouse X") * (Time.deltaTime * distance);
                        offset = Vector3.left * deltaX;
                        offset = new Vector3(-offset.x, 0.0f, 0.0f);
                        translateTarget.transform.Translate(offset);

                        float deltaZ = Input.GetAxis("Mouse Y") * (Time.deltaTime * distance);
                        offset = Vector3.forward * deltaZ;
                        offset = new Vector3(0.0f, 0.0f, offset.z);
                        translateTarget.transform.Translate(offset);
                    }
                    else
                    {
                        float delta = Input.GetAxis("Mouse Y") * (Time.deltaTime * distance);
                        offset = Vector3.up * delta;
                        offset = new Vector3(0.0f, offset.y, 0.0f);
                        translateTarget.transform.Translate(offset);
                    }
                }
                break;

                // Z Axis
                case 2:
                {
                    // If the user is pressing the plane, move along X and Y, else just move along Z

                    if (detectors[i].pressingPlane)
                    {
                        float deltaX = Input.GetAxis("Mouse X") * (Time.deltaTime * distance);
                        offset = Vector3.left * deltaX;
                        offset = new Vector3(-offset.x, 0.0f, 0.0f);
                        translateTarget.transform.Translate(offset);

                        float deltaY = Input.GetAxis("Mouse Y") * (Time.deltaTime * distance);
                        offset = Vector3.up * deltaY;
                        offset = new Vector3(0.0f, offset.y, 0.0f);
                        translateTarget.transform.Translate(offset);
                    }
                    else
                    {
                        float delta = Input.GetAxis("Mouse X") * (Time.deltaTime * distance);
                        offset = Vector3.forward * -delta;
                        offset = new Vector3(0.0f, 0.0f, offset.z);
                        translateTarget.transform.Translate(offset);
                    }
                }
                break;
                }

                // Move the gizmo to match the target position
                transform.position = translateTarget.transform.position;

                break;
            }
            else
            {
                isInteracting = false;
            }
        }
    }