コード例 #1
0
    void Update()
    {
        animationProgress += Time.deltaTime / animationDuration;

        float transitionSpeed = 7.0f * Time.deltaTime;

        // normalized mouse/finger position on the screen
        Vector3 pos = new Vector3();

        {         // Calculate the normalized position in the Card center area
            Vector3 npos = Input.mousePosition;

            // normalized position in the area
            npos.x = 2.0f * (npos.x - Screen.width / 2.0f) / (0.4f * Screen.height);
            npos.y = 2.0f * (npos.y - Screen.height / 2.0f) / Screen.height;

            npos.x = Mathf.Clamp(npos.x, -1.0f, 1.0f);
            npos.y = Mathf.Clamp(npos.y, -1.0f, 1.0f);

            // move the current mouse center
            if (!mouseDragging)
            {
                draggingCenter = npos;
            }

            // try to move starting center towards screen center
            draggingCenter.x = HomeToCenter(draggingCenter.x, npos.x, transitionSpeed);
            draggingCenter.y = HomeToCenter(draggingCenter.y, npos.y, transitionSpeed);

            // calculate position relative to dragging center
            pos.x = ValueRelativeTo(draggingCenter.x, npos.x);
            pos.y = ValueRelativeTo(draggingCenter.y, npos.y);
        }

        {         // Update mouse and handle trigger
            bool pressed  = Input.GetButtonDown("Fire1");
            bool released = Input.GetButtonUp("Fire1");
            if (pressed)
            {
                mouseDragging = true;
            }
            if (released)
            {
                mouseDragging = false;

                if (released)
                {
                    if (pos.x < -MouseThreshold)
                    {
                        Trigger(1);
                    }
                    else if (pos.x > MouseThreshold)
                    {
                        Trigger(-1);
                    }
                    else
                    {
                        Trigger(0);
                    }
                }
            }
        }

        {         // Update Card position
            if (Animating())
            {
                float p      = animationCurve.Evaluate(animationProgress);
                XForm target = XForm.Lerp(cardAnimationStart, cardAnimationFinish, p);
                target.MoveObject(Card, transitionSpeed);
            }
            else
            {
                XForm center = cardAnimationFinish;
                XForm tilted = new XForm(center);

                float tilt = Mathf.Abs(pos.x);
                if (mouseDragging)
                {
                    switch (state)
                    {
                    case State.Image:
                        tilted = pos.x < 0 ?
                                 center.TransformXZRY(0.0f, 0.0f, TiltRotation) :
                                 center.TransformXZRY(0.0f, 0.0f, -TiltRotation);
                        break;

                    case State.Description:
                    case State.Image2:
                        tilted = pos.x < 0 ?
                                 center.TransformXZRY(-TiltOffset, -0.5f, -TiltRotation) :
                                 center.TransformXZRY(TiltOffset, -0.5f, TiltRotation);

                        if (tilt > MouseThreshold)
                        {
                            tilt = 1.0f;
                        }
                        break;

                    case State.Option:
                    case State.Option2:
                        tilted = XForm.Lerp(center, new XForm(CardFinish), 0.5f);
                        tilt   = Mathf.Abs(pos.y) + Mathf.Abs(pos.x);
                        break;
                    }
                }

                XForm target = XForm.Lerp(center, tilted, tilt);
                target.MoveObject(Card, transitionSpeed);
            }
        }

        {         // Update Yes/No
            UnityEngine.UI.Text yesText = Yes.GetComponentInChildren <UnityEngine.UI.Text> ();
            UnityEngine.UI.Text noText  = No.GetComponentInChildren <UnityEngine.UI.Text> ();

            var adjustedYesZero = new XForm(yesZero);
            var adjustedNoZero  = new XForm(noZero);
            if (cameraController.IsWide())
            {
                adjustedYesZero.rotation   = Quaternion.Euler(new Vector3(0, 0, 0));
                adjustedYesZero.position.y = 0;
                adjustedNoZero.rotation    = Quaternion.Euler(new Vector3(0, 0, 0));
                adjustedNoZero.position.y  = 0;
            }

            XForm yesStart = new XForm(adjustedYesZero);
            XForm noStart  = new XForm(adjustedNoZero);

            XForm yesFinish = new XForm(adjustedYesZero);
            XForm noFinish  = new XForm(adjustedNoZero);

            float yesAlpha = 0.0f;
            float noAlpha  = 0.0f;

            if (state == State.Description || state == State.Image2)
            {
                if (pos.x < 0.0f)
                {
                    yesFinish            = new XForm(cardCenter);
                    yesFinish.position.x = -0.1f;
                }
                else
                {
                    noFinish            = new XForm(cardCenter);
                    noFinish.position.x = 0.1f;
                }

                if (pos.x < -MouseThreshold)
                {
                    yesAlpha = 1.0f;
                }
                else if (pos.x > MouseThreshold)
                {
                    noAlpha = 1.0f;
                }
                else
                {
                    yesAlpha = 0.5f;
                    noAlpha  = 0.5f;
                }
            }

            LerpAlphaTowards(yesText, yesAlpha, transitionSpeed * 0.5f);
            LerpAlphaTowards(noText, noAlpha, transitionSpeed * 0.5f);

            float p = Mathf.Abs(pos.x);
            if (p > MouseThreshold)
            {
                p = 1.0f;
            }
            XForm yesTarget = XForm.Lerp(yesStart, yesFinish, p);
            XForm noTarget  = XForm.Lerp(noStart, noFinish, p);

            yesTarget.MoveObject(Yes, transitionSpeed);
            noTarget.MoveObject(No, transitionSpeed);
        }
    }