상속: MonoBehaviour
예제 #1
0
    void LateUpdate()
    {
#if UNITY_ANDROID || UNITY_IOS
        float pinchAmount = 0;

        DetectTouchMovement.Calculate();

        if (Mathf.Abs(DetectTouchMovement.pinchDistanceDelta) > 0)
        { // zoom
            pinchAmount = DetectTouchMovement.pinchDistanceDelta;
        }

        Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, Camera.main.orthographicSize - pinchAmount, Time.deltaTime * 10);
#endif

#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
        Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, Camera.main.orthographicSize + Input.GetAxis("Mouse ScrollWheel") * -1200, Time.deltaTime * 10);
#endif

        if (Camera.main.orthographicSize > 200)
        {
            Camera.main.orthographicSize = 200;
        }

        if (Camera.main.orthographicSize < 10)
        {
            Camera.main.orthographicSize = 10;
        }
    }
예제 #2
0
    void calculateRotationAndZoom()
    {
        //rotate and zoom
        float pinchAmount = 0;

        DetectTouchMovement.Calculate();

        if (Mathf.Abs(DetectTouchMovement.pinchDistanceDelta) > 0)           // zoom
        {
            pinchAmount = DetectTouchMovement.pinchDistanceDelta;

            float moveRate = -1f;
            float zPos     = targetItem.transform.parent.parent.localPosition.z;
            float newZpos  = zPos + (pinchAmount * moveRate);
            //limit the zoom
            if (newZpos < 0.0f && newZpos > -800f)
            {
                targetItem.transform.parent.parent.localPosition += Vector3.forward * pinchAmount * moveRate;
            }
        }

        if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)           // rotate
        {
            float rotationDegZ = DetectTouchMovement.turnAngleDelta;
            //limit the twist

            //only rotate in Z if within the restricted amount
            //LimitedRotate(0, 0, rotationDegZ);
        }
    }
예제 #3
0
    private void LateUpdate()
    {
        float pinchAmount = 1.0f;

        DetectTouchMovement.Calculate();

        if (Mathf.Abs(DetectTouchMovement.pinchDistanceDelta) > 1)
        { // zoom
            if (DetectTouchMovement.pinchDistanceDelta > 0)
            {
                pinchAmount = 1.01f;
            }
            else
            {
                pinchAmount = 0.99f;
            }


            selectDataStream.transform.localScale *= pinchAmount;
        }

        if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
        { // rotate
            Vector3 rotationDeg = Vector3.zero;
            rotationDeg.y = -DetectTouchMovement.turnAngleDelta;
            selectDataStream.transform.Rotate(rotationDeg);
        }
    }
예제 #4
0
    //rotate object
    void ObjectRotationUpdate(GameObject draggedObject)
    {
        Quaternion desiredRotation = draggedObject.transform.rotation;

        DetectTouchMovement.Calculate();
        if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
        { // rotate
            Vector3 rotationDeg = Vector3.zero;
            rotationDeg.z    = DetectTouchMovement.turnAngleDelta;
            desiredRotation *= Quaternion.Euler(rotationDeg);
        }
        draggedObject.transform.rotation = desiredRotation;
    }
예제 #5
0
    void Update()
    {
        if (Input.touchCount < 1)
        {
            return;
        }

        Touch touch;

        DetectTouchMovement.Calculate();

        touch = Input.GetTouch(0);

        Ray        ray = Camera.main.ScreenPointToRay(touch.position);
        RaycastHit hit;

        if (!isPlacingRamp && Physics.Raycast(ray, out hit, float.MaxValue, rampLayerMask) && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            currentRamp = hit.transform.gameObject;
            SetEditButtons(true);
        }
        else if (currentRamp != null && !isPlacingRamp && Physics.Raycast(ray, out hit, float.MaxValue, ~rampLayerMask) && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            currentRamp = null;
            SetEditButtons(false);
        }


        if (!isPlacingRamp)
        {
            return;
        }

        if (Physics.Raycast(ray, out hit, float.MaxValue, planeLayerMask) && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            //Vector3 scaledHitPosition = hit.point;

            //scaledHitPosition.Scale(ScaleManager.instance.rootTransform.localScale);

            //scaledHitPosition = scaledHitPosition + (ScaleManager.instance.rootTransform.position - Sceeobjcet.transform.position);

            currentRamp.transform.position = hit.point;// new Vector3(scaledHitPosition.x, hit.point.y, scaledHitPosition.z);

            if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
            {
                Vector3 rotationDeg = Vector3.zero;
                rotationDeg.y = -DetectTouchMovement.turnAngleDelta * 2;
                currentRamp.transform.rotation *= Quaternion.Euler(rotationDeg);
            }
        }
    }
예제 #6
0
    void LateUpdate()
    {
        float      pinchAmount     = 0;
        Quaternion desiredRotation = transform.rotation;

        DetectTouchMovement.Calculate();

        if (Mathf.Abs(DetectTouchMovement.pinchDistanceDelta) > 0)
        {
            // zoom
            pinchAmount = DetectTouchMovement.pinchDistanceDelta;
        }

        if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
        {
            // rotate
            Vector3 rotationDeg = Vector3.zero;
            rotationDeg.y    = -DetectTouchMovement.turnAngleDelta;
            desiredRotation *= Quaternion.Euler(rotationDeg);
        }


        // not so sure those will work:
        transform.rotation = desiredRotation;
        //transform.position += Vector3.forward * pinchAmount;

        if (Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Ended)
            {
                TapCount += 1;
            }

            if (TapCount == 1)
            {
                NewTime = Time.time + MaxDubTapTime;
            }
            else if (TapCount == 2 && Time.time <= NewTime)
            {
                //Whatever you want after a dubble tap
                print("Dubble tap");
                gameObject.SetActive(false);

                TapCount = 0;
            }
        }
    }
    /// <summary>
    /// http://wiki.unity3d.com/index.php/DetectTouchMovement
    /// </summary>
    void LateUpdate()
    {
        if (isRotationAllowed == true)
        {
            if (buttonConnection.TouchPlaceScript.ItemTouchController != null)
            {
                // is this the current object selected in the TouchPlaceItem
                if (buttonConnection.TouchPlaceScript.ItemTouchController.ItemToSetIntoPlacer == this.gameObject)
                {
                    float      pinchAmount     = 0;
                    Quaternion desiredRotation = transform.rotation;

                    DetectTouchMovement.Calculate();

                    if (Mathf.Abs(DetectTouchMovement.pinchDistanceDelta) > 0)
                    { // zoom
                        pinchAmount = DetectTouchMovement.pinchDistanceDelta;
                    }

                    if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
                    { // rotate
                        Vector3 rotationDeg = Vector3.zero;
                        rotationDeg.y    = -DetectTouchMovement.turnAngleDelta;
                        desiredRotation *= Quaternion.Euler(rotationDeg);
                    }

                    if (Input.touchCount == 2)
                    {
                        isAutoRotateOn = false;
                    }


                    desiredRotation.x  = 0;
                    desiredRotation.z  = 0;
                    transform.rotation = desiredRotation;
                    pinchAmount        = pinchAmount * 0.001f;
                    Vector3 newScale = transform.localScale;
                    newScale += pinchAmount * transform.localScale;
                    if (newScale.x > 0.5 && newScale.x < 2)
                    {
                        transform.localScale = newScale;
                    }
                }
            }
        }
    }
    // Update is called once per frame
    void LateUpdate()
    {
        Quaternion desiredRotation = transform.rotation;

        DetectTouchMovement.Calculate();

        if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
        {
            Vector3 rotationDeg = Vector3.zero;
            rotationDeg.y    = -DetectTouchMovement.turnAngleDelta;
            desiredRotation *= Quaternion.Euler(rotationDeg);
        }

        // not so sure those will work:
        transform.rotation = desiredRotation;
        //transform.position += Vector3.forward * pinchAmount;
    }
예제 #9
0
    public void Update()
    {
        if (Input.touchCount <= 0)
        {
            return;
        }

        Touch touch = Input.GetTouch(0);

        Ray        ray = Camera.main.ScreenPointToRay(touch.position);
        RaycastHit hit;

        if (!isPlacingRamp && Physics.Raycast(ray, out hit, float.MaxValue, rampLayerMask) && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            currentRamp = hit.transform.gameObject;
            SetEditButtons(true);
        }
        else if (currentRamp != null && !isPlacingRamp && Physics.Raycast(ray, out hit, float.MaxValue, ~rampLayerMask) && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            currentRamp = null;
            SetEditButtons(false);
        }

        if (!isPlacingRamp)
        {
            return;
        }

        DetectTouchMovement.Calculate();

        if (Physics.Raycast(ray, out hit, float.MaxValue, planeLayerMask) && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            currentRamp.transform.position = hit.point;

            if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
            {
                Vector3 rotationDeg = Vector3.zero;
                rotationDeg.y = DetectTouchMovement.turnAngleDelta * 2;
                currentRamp.transform.rotation *= Quaternion.Euler(rotationDeg);
            }
        }
    }
예제 #10
0
    // We do all the panrotationg touch logic in lateupdate
    private void LateUpdate()
    {
        if (spawnedObject && !scriptableManager.ControlState.IsRelocating)
        {
            initialScale = arSessionOrigin.transform.localScale;

            DetectTouchMovement.Calculate();

            if (Mathf.Abs(DetectTouchMovement.pinchFactor) > 0.0f)
            {
                // target scale
                targetScale = initialScale * DetectTouchMovement.pinchFactor;
            }

            if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0.0f)
            {
                // target rotation
                Vector3 rotationDeg = Vector3.zero;
                rotationDeg.y   = -DetectTouchMovement.turnAngleDelta;
                targetRotation *= Quaternion.Euler(rotationDeg);
            }
        }
    }
예제 #11
0
    void LateUpdate()
    {
        float      pinchAmount     = 0;
        Quaternion desiredRotation = transform.rotation;

        DetectTouchMovement.Calculate();

        if (Mathf.Abs(DetectTouchMovement.pinchDistanceDelta) > 0)
        { // zoom
            pinchAmount = DetectTouchMovement.pinchDistanceDelta;
        }

        if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
        { // rotate
            Vector3 rotationDeg = Vector3.zero;
            rotationDeg.z    = -DetectTouchMovement.turnAngleDelta;
            desiredRotation *= Quaternion.Euler(rotationDeg);
        }


        // not so sure those will work:
        transform.rotation  = desiredRotation;
        transform.position += Vector3.forward * pinchAmount;
    }
예제 #12
0
    void Update()
    {
        for (int i = 0; i < popUps.Length; i++)
        {
            if (i == popUpIndex)
            {
                popUps[i].SetActive(true);
            }
            else
            {
                popUps[i].SetActive(false);
            }
        }

        //adapted touchrotate script here (actions are stopped by flags depending on tutorial progress)
        if ((Input.touchCount > 0) && (translateFlag == true))                                                                            //at least one touch detected
        {
            wp       = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);                                                        //Vector3 of the touch location on the screen
            touchPos = new Vector2(wp.x, wp.y);

            GameObject go = GameObject.Find("RedTriangle");

            if (go)
            {
                myCollider = go.gameObject.GetComponent <CircleCollider2D>();


                if (myCollider)                                                                                                         //collider will not exist for the anchor shape0
                {
                    if (myCollider == Physics2D.OverlapPoint(touchPos))                                                                 //if the touch position overlaps with the 2D collider of the shape
                    {
                        if (Input.touchCount == 1)
                        {                                                                                                                                                                          //updates shape translated position https://answers.unity.com/questions/991083/dragging-a-2d-sprite-with-touch.html
                            if (((Camera.main.ScreenToWorldPoint(Input.mousePosition).y < Camera.main.ScreenToWorldPoint(GameObject.Find("MaxHeight").transform.position).y) && (popUpIndex < 9)) ||
                                ((popUpIndex == 9) && (Camera.main.ScreenToWorldPoint(Input.mousePosition).x < Camera.main.ScreenToWorldPoint(GameObject.Find("MaxWidth").transform.position).x))) // stops shape going behind instructions
                            {
                                go.gameObject.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
                                                                               Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
                                                                               0.0f);
                                if (popUpIndex == 6)
                                {
                                    popUpIndex = 7;
                                    TuteLoadManager();
                                }
                            }
                        }

                        if ((Input.touchCount == 2) && (rotateFlag == true))
                        {
                            Quaternion desiredRotation = go.gameObject.transform.rotation;                                           //start desiredRotation as the current orientation of the shape
                            DetectTouchMovement.Calculate();                                                                         //determines turnAngle and turnAngleDelta from 2 finger rotation on screen

                            if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)                                                   //if the detected turn angle is large enough and the shape is not small/circular
                            {
                                Vector3 rotationDeg = Vector3.zero;
                                rotationDeg.z    = DetectTouchMovement.turnAngleDelta;
                                desiredRotation *= Quaternion.Euler(rotationDeg);                                                        //update the desiredRotation to include this change in angle

                                go.gameObject.transform.rotation = desiredRotation;                                                      //upate the shape rotated orientation

                                if (popUpIndex == 7)
                                {
                                    popUpIndex = 8;
                                    TuteLoadManager();
                                }
                            }
                        }
                        //https://answers.unity.com/questions/369230/how-to-detect-double-tap-in-android.html?childToView=1695525#answer-1695525
                    }
                }
            }
        }
        else
        { //no touch counts
            GameObject go = GameObject.Find("RedTriangle");

            if (go)
            {
                if ((go.transform.position.y < Camera.main.ScreenToWorldPoint(GameObject.Find("Divider").transform.position).y) && (toolbarFlag == true))
                {
                    //if released in the toolbar return to correct place and orientation
                    go.transform.position = new Vector3(-0.0159f, Global.toolbarY, 0f);
                    go.transform.rotation = Quaternion.Euler(0f, 0f, 0f);

                    if (popUpIndex == 8)
                    {
                        popUpIndex = 9;
                        TuteLoadManager();
                    }
                }
                else if (((TargetPosition.x - Global.positionTolerance) < go.transform.position.x && go.transform.position.x < (TargetPosition.x + Global.positionTolerance)
                          & (TargetPosition.y - Global.positionTolerance) < go.transform.position.y && go.transform.position.y < (TargetPosition.y + Global.positionTolerance)
                          & (TargetRotation.z - Global.rotationTolerance) < go.transform.rotation.eulerAngles.z && go.transform.rotation.eulerAngles.z < (TargetRotation.z + Global.rotationTolerance)) && (completeFlag == true))
                {
                    go.transform.position = TargetPosition;                            //snap to position
                    go.transform.rotation = Quaternion.Euler(TargetRotation);          //snap to orientation


                    if (popUpIndex == 9)
                    {
                        popUpIndex = 10;
                        TuteLoadManager();
                    }
                }
            }
        }
    }
예제 #13
0
    void Update()
    {
//        Debug.DrawRay(Vector3.zero, Vector3.Cross(mainCamera.transform.forward, movableObject.right),Color.blue);


        if (Input.touchCount <= 0)
        {
            return;
        }

        // calculate which panel is being pressed
        if (Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch(0);

            if (touch.phase.Equals(TouchPhase.Began))
            {
                Vector2 worldPoint = mainCamera.ScreenToWorldPoint(touch.position);

                // store the first finger position
                previousTouchPosition = touch.position;
            }
            else if (touch.phase.Equals((TouchPhase.Moved)))
            {
                if (adjustingXForce)
                {
                    // camera's look at direction determines where the finger must move to to adjust the x force

                    float adjustableValueX = Vector3.Cross(mainCamera.transform.forward, movableObject.right).y > 0
                        ? touch.position.x - previousTouchPosition.x
                        : previousTouchPosition.x - touch.position.x;

                    Vector2 xDifference = new Vector2(adjustableValueX / xForceImageArea.sizeDelta.x, 0);
                    AdjustLineRendererPosition(xForceRenderer, xDifference, Vector2.right, out xForceToObject);
                }
                else if (adjustingYForce)
                {
                    Vector2 yDifference = new Vector2(0, (touch.position.y - previousTouchPosition.y) / yForceImageArea.sizeDelta.y);
                    AdjustLineRendererPosition(yForceRenderer, yDifference, Vector2.up, out yForceToObject);
                }

                previousTouchPosition = touch.position;
            }
            else if (touch.phase.Equals((TouchPhase.Ended)))
            {
                adjustingXForce = false;
                adjustingYForce = false;
            }
        }
        else if (Input.touchCount == 2)
        {
            adjustingXForce = false;
            adjustingYForce = false;

            Quaternion desiredRotation = movableObject.rotation;
            DetectTouchMovement.Calculate();

            if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
            {
                // rotate
                Vector3 rotationDeg = Vector3.zero;
                rotationDeg.z    = -DetectTouchMovement.turnAngleDelta;
                desiredRotation *= Quaternion.Euler(rotationDeg);
            }

            movableObject.Rotate(Vector3.up, DetectTouchMovement.turnAngleDelta);
            lastAdjustedRotation = movableObject.rotation;
        }
    }
예제 #14
0
    // Update is called once per frame
    void LateUpdate()
    {
        if (EventSystem.current.IsPointerOverGameObject() ||
            EventSystem.current.currentSelectedGameObject != null)
        {
            return;
        }


        if (Application.isEditor)
        {
            if (Input.GetMouseButton(0))
            {
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit, 500f, layerMask))
                {
                    currentSelected = hit.collider.gameObject.transform.parent.gameObject;
                }
            }
        }
        else
        {
            if (Input.touchCount == 1)
            {
                Touch touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Began)
                {
                    Ray        ray = Camera.main.ScreenPointToRay(touch.position);
                    RaycastHit hit;

                    if (Physics.Raycast(ray, out hit, 500f, layerMask))
                    {
                        currentSelected = hit.collider.gameObject.transform.parent.gameObject;
                    }
                }
            }
            else if (Input.touchCount == 2)
            {
                Touch touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Moved)
                {
                    if (currentSelected != null)
                    {
                        if (currentSelected.activeSelf == true)
                        {
                            float      pinchAmount     = 0;
                            Quaternion desiredRotation = currentSelected.transform.rotation;

                            DetectTouchMovement.Calculate();

                            if (Mathf.Abs(DetectTouchMovement.pinchDistanceDelta) > 0)
                            { // zoom
                                pinchAmount = DetectTouchMovement.pinchDistanceDelta;
                            }

                            if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)
                            { // rotate
                                Vector3 rotationDeg = Vector3.zero;
                                rotationDeg.y    = -DetectTouchMovement.turnAngleDelta;
                                desiredRotation *= Quaternion.Euler(rotationDeg);
                            }

                            desiredRotation.x = 0;
                            desiredRotation.z = 0;

                            // will work:
                            currentSelected.transform.rotation = desiredRotation;

                            pinchAmount = pinchAmount * 0.001f;
                            Vector3 newScale = currentSelected.transform.localScale;

                            newScale += pinchAmount * currentSelected.transform.localScale;

                            if (newScale.x > 0.5 && newScale.x < 2)
                            {
                                currentSelected.transform.localScale = newScale;
                            }
                        }
                    }
                }
            }
        }
    }
예제 #15
0
    //void update()
    //{
    //    if (input.getmousebuttondown(0))
    //    {
    //        dragorigin = input.mouseposition;
    //        return;
    //    }

    //    if (!input.getmousebutton(0)) return;

    //    vector3 pos = camera.main.screentoviewportpoint(input.mouseposition - dragorigin);
    //    vector3 move = new vector3(pos.x * dragspeed, 0, pos.y * dragspeed);

    //    transform.translate(move, space.world);
    //    }


    // Update is called once per frame
    void LateUpdate()
    {
        if (isEndDay)
        {
            return;
        }

        if (IsPointerOverUIObject())
        {
            return;
        }

        Quaternion desiredRotation = transform.rotation;

        DetectTouchMovement.Calculate();

        //debugText8.text = "eulerAngles : " + transform.localRotation.eulerAngles;
        debugText9.text = "position : " + transform.position;

        if (Input.touchCount == 0 && (isZooming || isRotating))
        {
            isZooming  = false;
            isRotating = false;
        }

        if (Input.touchCount == 1)
        {
            if (!isZooming && !isRotating && !isBuilding)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    Vector2 NewPosition        = GetWorldPosition();
                    Vector3 PositionDifference = NewPosition - StartPosition;
                    //Vector2 PositionDifference = (NewPosition - StartPosition).normalized;
                    Vector3 vec = new Vector3(PositionDifference.x, PositionDifference.y, 0);
                    //transform.Translate(-PositionDifference);
                    transform.Translate(-PositionDifference * moveSpeed * Time.deltaTime);
                    //transform.position -= vec * moveSpeed * Time.deltaTime;
                    debugText7.text = "NewPosition : " + NewPosition;
                    debugText8.text = "StartPosition : " + StartPosition;
                }
                StartPosition = GetWorldPosition();
            }
        }
        else if (Input.touchCount == 2)
        {
            debugText.text  = "turnAngleDelta : " + Mathf.Abs(DetectTouchMovement.turnAngleDelta);
            debugText2.text = "pinchDistanceDelta : " + Mathf.Abs(DetectTouchMovement.pinchDistanceDelta);
            debugText3.text = "isRotating : " + isRotating;
            debugText4.text = "isZooming  : " + isZooming;

            if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0.5 && !isZooming)  //rotate
            {
                isRotating     = true;
                debugText.text = "isRotating  : " + isRotating;
            }
            if (Mathf.Abs(DetectTouchMovement.pinchDistanceDelta) > 5 && !isRotating)   //zoom
            {
                isZooming = true;

                Touch touchZero = Input.GetTouch(0);
                Touch touchOne  = Input.GetTouch(1);

                Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
                Vector2 touchOnePrevPos  = touchOne.position - touchOne.deltaPosition;

                float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
                float touchDeltaMag     = (touchZero.position - touchOne.position).magnitude;

                float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

                debugText5.text = "fieldOfView : " + GetComponent <Camera>().fieldOfView;
                debugText8.text = "deltaMagnitudeDiff" + deltaMagnitudeDiff;

                if (maxZoom >= GetComponent <Camera>().fieldOfView&& deltaMagnitudeDiff < 0)
                {
                    debugText6.text    = "true bigger";
                    deltaMagnitudeDiff = 0;
                }

                if (minZoom <= GetComponent <Camera>().fieldOfView&& deltaMagnitudeDiff > 0)
                {
                    deltaMagnitudeDiff = 0;
                }

                if (GetComponent <Camera>().orthographic)
                {
                    GetComponent <Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
                    GetComponent <Camera>().orthographicSize  = Mathf.Max(GetComponent <Camera>().orthographicSize, 0.1f);
                }
                else
                {
                    GetComponent <Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
                    GetComponent <Camera>().fieldOfView  = Mathf.Clamp(GetComponent <Camera>().fieldOfView, 0.1f, 179.9f);
                }

                //DragNewPosition = GetWorldPositionOfFinger(1);
                //Vector2 PositionDifference = DragNewPosition - DragStartPosition;
                //float tempMagnitude;
                //    tempMagnitude = Mathf.Clamp(PositionDifference.magnitude, 0.01f, 1);
                //debugText5.text = "move.magnitude? : ";
                //debugText6.text = "tempMagnitude? : " + tempMagnitude;
                //if (tempMagnitude > 0.8)
                //{
                //    tempMagnitude = Mathf.Clamp(tempMagnitude, 0.01f, 0.5f);
                //    //return;
                //}
                //if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
                //{
                //    if (Vector2.Distance(DragNewPosition, Finger0Position) < DistanceBetweenFingers)
                //        GetComponent<Camera>().orthographicSize += (tempMagnitude);

                //    if (Vector2.Distance(DragNewPosition, Finger0Position) >= DistanceBetweenFingers)
                //        GetComponent<Camera>().orthographicSize -= (tempMagnitude);
                //}
                //DistanceBetweenFingers = Vector2.Distance(DragNewPosition, Finger0Position);

                //DragStartPosition = GetWorldPositionOfFinger(1);
                //Finger0Position = GetWorldPositionOfFinger(0);
            }

            //if (isRotating)
            //    RotateCamera();
            //else if (isZooming)
            //ZoomCamera();
        }
    }
예제 #16
0
    void Update()
    {
        if (Global.PieceActive == true)                                                          //determines whether toolbar arrows are active or not
        {
            GameObject.Find("LeftArrow").GetComponent <Button>().interactable  = false;
            GameObject.Find("RightArrow").GetComponent <Button>().interactable = false;
        }
        else if (Global.PieceActive == false)
        {
            if (Global.LeftArrowActive == true)
            {
                GameObject.Find("LeftArrow").GetComponent <Button>().interactable = true;
            }
            else if (Global.LeftArrowActive == false)
            {
                GameObject.Find("LeftArrow").GetComponent <Button>().interactable = false;
            }

            if (Global.RightArrowActive == true)
            {
                GameObject.Find("RightArrow").GetComponent <Button>().interactable = true;
            }
            else if (Global.RightArrowActive == false)
            {
                GameObject.Find("RightArrow").GetComponent <Button>().interactable = false;
            }
        }


        //adapted code from http://wiki.unity3d.com/index.php/DetectTouchMovement
        if (Input.touchCount > 0)                                                                                                         //at least one touch detected
        {
            wp       = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);                                                        //Vector3 of the touch location on the screen
            touchPos = new Vector2(wp.x, wp.y);                                                                                           //Vector2 format of touch location

            foreach (string name in nameArray)
            {
                GameObject go = GameObject.Find(name);                                                                                     //checks if the shape exists

                if (go)
                {
                    if ((activeArray[System.Array.IndexOf(nameArray, go.name)] == true && Global.PieceActive == false) || (Global.ActiveName == go.name && Global.PieceActive == true)) //if shape is active
                    {                                                                                                                                                                   //the shape is active
                        myCollider = go.gameObject.GetComponent <CircleCollider2D>();

                        if (myCollider)                                                                                                     //collider will not exist for the anchor shape0
                        {
                            if (myCollider == Physics2D.OverlapPoint(touchPos))                                                             //if the touch position overlaps with the 2D collider of the shape
                            {
                                if (Input.touchCount == 2 && circleArray[System.Array.IndexOf(nameArray, go.name)] == false && smallArray[System.Array.IndexOf(nameArray, go.name)] == false)
                                {                                                                                                            //updates rotation
                                    Quaternion desiredRotation = go.gameObject.transform.rotation;                                           //start desiredRotation as the current orientation of the shape
                                    DetectTouchMovement.Calculate();                                                                         //determines turnAngle and turnAngleDelta from 2 finger rotation on screen

                                    if (Mathf.Abs(DetectTouchMovement.turnAngleDelta) > 0)                                                   //if the detected turn angle is large enough and the shape is not small/circular
                                    {
                                        Vector3 rotationDeg = Vector3.zero;
                                        rotationDeg.z    = DetectTouchMovement.turnAngleDelta;
                                        desiredRotation *= Quaternion.Euler(rotationDeg);                                                    //update the desiredRotation to include this change in angle

                                        go.gameObject.transform.rotation = desiredRotation;                                                  //upate the shape rotated orientation
                                    }
                                }
                                if (Input.touchCount == 1)
                                {
                                    //updates shape translated position
                                    //https://answers.unity.com/questions/991083/dragging-a-2d-sprite-with-touch.html
                                    go.gameObject.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
                                                                                   Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
                                                                                   0.0f);
                                }
                            }
                        }
                    }
                }
            }
        }
        else                                                                                                                                         //no touches
        {
            foreach (string name in nameArray)
            {
                GameObject go = GameObject.Find(name);                                                                                                 //checks if the shape exists


                if (go)
                {
                    if ((activeArray[System.Array.IndexOf(nameArray, go.name)] == true && Global.PieceActive == false) || (Global.ActiveName == go.name && Global.PieceActive == true))
                    {
                        Vector3 TargetPosition = positionArray[System.Array.IndexOf(nameArray, go.name)];
                        Vector3 TargetRotation = rotationArray[System.Array.IndexOf(nameArray, go.name)];

                        float TargetRotationOp = (TargetRotation.z + 180) % 360; ///


                        if ((TargetPosition.x - Global.positionTolerance) < go.transform.position.x && go.transform.position.x < (TargetPosition.x + Global.positionTolerance) &&
                            (TargetPosition.y - Global.positionTolerance) < go.transform.position.y && go.transform.position.y < (TargetPosition.y + Global.positionTolerance) &&
                            ((TargetRotation.z - Global.rotationTolerance) < go.transform.rotation.eulerAngles.z && go.transform.rotation.eulerAngles.z < (TargetRotation.z + Global.rotationTolerance) ||
                             ((go.GetComponent <SpriteRenderer>().sprite.name == "Square" || go.GetComponent <SpriteRenderer>().sprite.name == "Parallelogram") && (TargetRotationOp - Global.rotationTolerance) < go.transform.rotation.eulerAngles.z && go.transform.rotation.eulerAngles.z < (TargetRotationOp + Global.rotationTolerance))
                            ))                                                                                    //in the correct position, leave where it is

                        {                                                                                         //if shape is placed within tolerances
                            Global.PieceActive = false;

                            activeArray[System.Array.IndexOf(nameArray, go.name)] = false;
                            Global.piecesPlaced++;

                            for (int i = 0; i < toolbarArray.Length; i++)
                            {
                                if (i > System.Array.IndexOf(nameArray, go.name))
                                {
                                    toolbarArray[i] = toolbarArray[i] - Global.shapeOffset;
                                }
                            }

                            go.GetComponent <SpriteRenderer>().sortingLayerName = layerArray[System.Array.IndexOf(nameArray, go.name)]; //move to correct layer

                            go.GetComponent <CircleCollider2D>().enabled = false;                                                       //disable collider of placed shape

                            go.transform.position = TargetPosition;                                                                     //snap to position
                            go.transform.rotation = Quaternion.Euler(TargetRotation);                                                   //snap to orientation

                            Vector4 OldColour = go.GetComponent <SpriteRenderer>().color;
                            Vector4 NewColour = OldColour + Global.ColourOffset;

                            if (Global.SoundEffects == true)
                            {
                                FindObjectOfType <AudioManager>().Play("Correct");               //plays correct placement sound
                            }
                            StartCoroutine(FlashObject(go, OldColour, NewColour, 0.5f, 0.125f)); //make correclty placed piece flash

                            Global.CheckArrows();
                        }
                        else if (go.transform.position.y < Camera.main.ScreenToWorldPoint(GameObject.Find("Divider").transform.position).y)              //if piece is released inside toolbar
                        {
                            go.transform.position = toolbarArray[System.Array.IndexOf(nameArray, go.name)];
                            go.transform.rotation = Quaternion.Euler(toolbarRotationArray[System.Array.IndexOf(nameArray, go.name)]);

                            Global.PieceActive = false;
                        }
                        else                                                                                                                             //if piece is released in assembly area
                        {
                            if (Global.PieceActive == false)
                            {
                                Global.PieceActive = true;
                                Global.ActiveName  = go.name;
                            }
                        }
                    }
                }
            }
        }
    }//end update