• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by dhruvchadha1997 · Jul 07, 2020 at 05:17 PM · animationrotationtransformtweening

Simple way to create a flashcard-like flipping animation for UI?

I plan to use flashcards to ask questions to players. the question should be on the front of the card, and answer on the back.

Currently, Im using itween to ac$$anonymous$$eve t$$anonymous$$s. There are 2 panels superimposed on one another. The panel serving as the back of the card is rotated 180 deg. Using itween, im rotating both the panels, w$$anonymous$$ch gives a card rotating effect.

Also, halfway through the animation, im also doing backPanel.transform.SetAsFirstSibling(), so that the z-index issues are removed.

However, all t$$anonymous$$s is requiring lots of lines of code, and it looks ugly and complicated. Is there a simpler way to ac$$anonymous$$ve t$$anonymous$$s?

My code ->

  using UnityEngine;
  using UnityEngine.UI;
  
  public class CardController : MonoBehaviour
  {
      [SerializeField] private GameObject cardFront;
      [SerializeField] private GameObject cardBack;
      void Start()
      {
          cardFront.transform.GetC$$anonymous$$ld(0).gameObject.GetComponent<Button>().onClick.AddListener(HalfRotate);
          cardBack.transform.GetC$$anonymous$$ld(0).gameObject.GetComponent<Button>().onClick.AddListener(HalfReverseRotate);
      }
  
      void HalfRotate()
      {
          iTween.RotateTo(cardFront, iTween.Hash(
                  "rotation", new Vector3(0, 90, 0),
                  "time", 0.1f,
                  "easetype", iTween.EaseType.linear,
                  "onComplete", "FullRotate",
                  "onCompleteTarget", gameObject
                  ));
          iTween.RotateTo(cardBack, iTween.Hash(
              "rotation", new Vector3(0, 270, 0),
              "time", 0.1f,
              "easetype", iTween.EaseType.linear
              ));
      }
  
      void FullRotate()
      {
          cardFront.transform.SetAsFirstSibling();
          iTween.RotateTo(cardFront, iTween.Hash(
                  "rotation", new Vector3(0, 180, 0),
                  "time", 0.1f,
                  "easetype", iTween.EaseType.linear
                  ));
          iTween.RotateTo(cardBack, iTween.Hash(
                  "rotation", new Vector3(0, 360, 0),
                  "time", 0.1f,
                  "easetype", iTween.EaseType.linear
                  ));
      }
  
      void HalfReverseRotate()
      {
          iTween.RotateTo(cardFront, iTween.Hash(
                  "rotation", new Vector3(0, 90, 0),
                  "time", 0.1f,
                  "easetype", iTween.EaseType.linear,
                  "onComplete", "FullReverseRotate",
                  "onCompleteTarget", gameObject
                  ));
          iTween.RotateTo(cardBack, iTween.Hash(
              "rotation", new Vector3(0, 270, 0),
              "time", 0.1f,
              "easetype", iTween.EaseType.linear
              ));
      }
  
      void FullReverseRotate()
      {
          cardBack.transform.SetAsFirstSibling();
          iTween.RotateTo(cardFront, iTween.Hash(
                  "rotation", new Vector3(0, 0, 0),
                  "time", 0.1f,
                  "easetype", iTween.EaseType.linear
                  ));
          iTween.RotateTo(cardBack, iTween.Hash(
                  "rotation", new Vector3(0, 180, 0),
                  "time", 0.1f,
                  "easetype", iTween.EaseType.linear
                  ));
      }
  }


Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Slimer37 · Jul 08, 2020 at 06:33 AM

I can t$$anonymous$$nk of one way to shorten t$$anonymous$$s; I noticed a bunch of repeats with how you rotate the card faces, so you can try condensing the repeated parts (no guarantees that t$$anonymous$$s'll work):

 using UnityEngine;
 using UnityEngine.UI;
 
 public class CardController : MonoBehaviour
 {
     [SerializeField] private GameObject cardFront;
     [SerializeField] private GameObject cardBack;
     void Start()
     {
         cardFront.transform.GetC$$anonymous$$ld(0).gameObject.GetComponent<Button>().onClick.AddListener(HalfRotate);
         cardBack.transform.GetC$$anonymous$$ld(0).gameObject.GetComponent<Button>().onClick.AddListener(HalfReverseRotate);
     }
 
     void RotateFront(float degrees, string onComplete)
     {
         iTween.RotateTo(cardFront, iTween.Hash(
             "rotation", new Vector3(0, degrees, 0),
             "time", 0.1f,
             "easetype", iTween.EaseType.linear,
             "onComplete", onComplete,
             "onCompleteTarget", gameObject
             ));
     }
 
     void RotateFront(float degrees)
     {
         iTween.RotateTo(cardFront, iTween.Hash(
             "rotation", new Vector3(0, degrees, 0),
             "time", 0.1f,
             "easetype", iTween.EaseType.linear
             ));
     }
 
     void RotateBack(float degrees)
     {
         iTween.RotateTo(cardBack, iTween.Hash(
             "rotation", new Vector3(0, degrees, 0),
             "time", 0.1f,
             "easetype", iTween.EaseType.linear
             ));
     }
 
     void HalfRotate()
     {
         FullRotateFront(90);
         RotateBack(270);
     }
 
     void FullRotate()
     {
         cardFront.transform.SetAsFirstSibling();
         RotateFront(180);
         RotateBack(360);
     }
 
     void HalfReverseRotate()
     {
         RotateFront(90, "FullReverseRotate");
         RotateBack(270);
     }
 
     void FullReverseRotate()
     {
         cardBack.transform.SetAsFirstSibling();
         RotateFront(0);
         RotateBack(180);
     }
 }
Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Slimer37 · Jul 08, 2020 at 06:34 AM 1
Share

Besides that, I've no more ideas. This at least makes it more readable.

avatar image dhruvchadha1997 Slimer37 · Jul 08, 2020 at 08:03 AM 0
Share

i was looking for maybe to avoid all this, maybe a far shorter DIFFERENT approch?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

357 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animating sliding door, rotation problem 2 Answers

Bullet Script change direction with player (2D)? 1 Answer

Top-down character running animation based on facing direction 0 Answers

Wrong rotation while swimming 2 Answers

Door opening in the wrong direction..... 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges