Search code examples
unityscriptunity-game-engine

How to Diagonal Flip Animation in Unity


I am creating a card game in unity. In that, I need to flip a card in diagonal. I have tried using rotate and translate methods, but unfortunately I was unable to archive the target. I have attached the YouTube link with this thread. Anyone can help me to overcome this issue?

https://youtu.be/j5lBJYSSX2A


Solution

  • I have this code for card fliping. You will need to change the numbers of the rotation to rotate it diagonally but it should work for you

    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    
        }
    

    And here is the Tweeng function I use to do the smooth lerp of values :

    /// <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);
                }
    

    you can read more about it and how to use it from this question