Пример #1
0
    private void Update()
    {
        if (selected == null)
        {
            DetectObject();
            if (touchControls.DetectDoubleTap() && target != null)
            {
                selected = target;
                selected.transform.SetParent(Camera.main.transform);
                kinematicBuffer = selected.GetComponent <Rigidbody>().isKinematic;
                selected.GetComponent <Rigidbody>().isKinematic = true;
                selected.GetComponent <Collider>().isTrigger    = true;
            }
        }
        else if (selected != null && touchControls.DetectDoubleTap())
        {
            selected.transform.SetParent(null);

            selected.GetComponent <Rigidbody>().isKinematic = kinematicBuffer;
            selected.GetComponent <Collider>().isTrigger    = false;

            save.ChangeObject(selected);

            selected = null;
        }
        else
        {
            float deltaSize = touchControls.DetectPinch() / touchControls.pinchDamper;

            if (Mathf.Abs(deltaSize) > 0.01f)
            {
                float x = Mathf.Clamp(selected.transform.lossyScale.x + deltaSize, touchControls.scaleMin, touchControls.scaleMax);
                float y = Mathf.Clamp(selected.transform.lossyScale.y + deltaSize, touchControls.scaleMin, touchControls.scaleMax);
                float z = Mathf.Clamp(selected.transform.lossyScale.z + deltaSize, touchControls.scaleMin, touchControls.scaleMax);
                selected.transform.localScale = new Vector3(x, y, z);
            }

            Vector2 deltaScroll = touchControls.DetectScroll();

            if (deltaScroll.magnitude > 0.1f)
            {
                Vector3 rot = touchControls.GetRotationFromScreenSpaceValue(deltaScroll, selected.transform);
                selected.transform.Rotate(rot * touchControls.rotateSensitivity);
            }

            float deltaSwivel = touchControls.DetectSwivel();

            if (Mathf.Abs(deltaSwivel) > 0)
            {
                Vector3 fwdRot = touchControls.GetRotationFromSwivel(deltaSwivel, selected.transform);
                selected.transform.Rotate(fwdRot * touchControls.swivelSensitivity);
            }

            RaycastHit hit;
            Ray        ray = new Ray(selected.transform.position, new Vector3(0, -1, 0));
            if (Physics.Raycast(ray, out hit))
            {
                raycastIndicator.SetActive(true);

                raycastIndicator.transform.position = hit.point;
                raycastLine.SetPositions(new Vector3[] { raycastIndicator.transform.position, selected.transform.position });
            }
            else
            {
                raycastLine.SetPositions(new Vector3[] { raycastIndicator.transform.position, raycastIndicator.transform.position });
                raycastIndicator.SetActive(false);
            }
        }
    }