Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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 achieve this. 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, which 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 this is requiring lots of lines of code, and it looks ugly and complicated. Is there a simpler way to achive this?

My code ->

  using UnityEngine;
  using UnityEngine.UI;
  
  public class CardController : MonoBehaviour
  {
      [SerializeField] private GameObject cardFront;
      [SerializeField] private GameObject cardBack;
      void Start()
      {
          cardFront.transform.GetChild(0).gameObject.GetComponent<Button>().onClick.AddListener(HalfRotate);
          cardBack.transform.GetChild(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
Add comment
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
0

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

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

 using UnityEngine;
 using UnityEngine.UI;
 
 public class CardController : MonoBehaviour
 {
     [SerializeField] private GameObject cardFront;
     [SerializeField] private GameObject cardBack;
     void Start()
     {
         cardFront.transform.GetChild(0).gameObject.GetComponent<Button>().onClick.AddListener(HalfRotate);
         cardBack.transform.GetChild(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
Add comment · 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.

Follow this Question

Answers Answers and Comments

358 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 avatar image

Related Questions

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

How do I add rotation according to mouse movement to a character even though animation is being played? 0 Answers

Adding child repositions parent tranform 1 Answer


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