如何在Unity中对角线翻转动画

How to Diagonal Flip Animation in Unity

我正在统一创建一个纸牌游戏。在那,我需要在对角线上翻转一张卡片。我曾尝试使用旋转和平移方法,但不幸的是我无法存档目标。我已将 YouTube link 附加到此主题。谁能帮我解决这个问题?

https://youtu.be/j5lBJYSSX2A

我有这个翻牌代码。您将需要更改旋转次数以沿对角线旋转它,但它应该适合您

IEnumerator FlipCard()
    {

        yield return StartCoroutine(Constants.CardFlipTime.Tweeng((u) => gameObject.transform.localEulerAngles = new Vector3(0f, u, 0f),0, 90f));//begin the rotation
        GetComponent<Image>().sprite = cardFrame;// change the card sprite since it currently not visible 

        Debug.Log("Rotated 90 deg");
        yield return StartCoroutine(Constants.CardFlipTime.Tweeng((u) => gameObject.transform.localEulerAngles = new Vector3(0f, u, 0f), 90, 0f));//finish the rotation    
    }

这是我用来对值进行平滑 lerp 的 Tweeng 函数:

/// <summary>
            /// Generic static method for asigning a action to any type
            /// </summary>
            /// <typeparam name="T"> generic</typeparam>
            /// <param name="duration">Method is called on the float and same float is used as the duration</param>
            /// <param name="vary">Action to perform over given duration</param>
            /// <param name="start">Starting value for the action</param>
            /// <param name="stop">End value of the action</param>
            /// <returns>null</returns>
            public static IEnumerator Tweeng<T>(this float duration, Action<T> vary,T start, T stop)
            {
                float sT = Time.time;
                float eT = sT + duration;
                Delegate d;
                if (typeof(T) == typeof(float))
                    d = (Func<float, float, float, float>)Mathf.SmoothStep;
                else if (typeof(T) == typeof(Vector3))
                    d = (Func<Vector3, Vector3, float, Vector3>)Vector3.Lerp;
                else if (typeof(T) == typeof(Quaternion))
                    d = (Func<Quaternion, Quaternion, float, Quaternion>)Quaternion.RotateTowards;
                else
                    throw new ArgumentException("Unexpected type " + typeof(T));

                Func<T, T, float, T> step = (Func<T, T, float, T>)d;
                while (Time.time < eT)
                {
                    float t = (Time.time - sT) / duration;
                    vary(step(start, stop, t));
                    yield return null;
                }
                vary(stop);
            }

您可以从 问题

中了解更多信息以及如何使用它