Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by andracer108 · Dec 11, 2016 at 08:41 AM · c#rotationquaternionplane

Smooth card rotation with Quaternion not doing anything

Summary

I am currently making a small game of match the pairs in Unity. To create the 'cards' I used 2 planes and put them back to back and grouped them in an empty object called Card. I created a prefab from this and duplicated them with an ID with them for example: Card1, Card2, Card3 etc.

In the CardScript.cs file which is attached as a component on every prefab is responsible for the movement of these cards. I started the scene by showing the player the cards (front side up) and then turn 190 degrees after a delay. When the player hovers over the cards, the card's material tint changes colour and when the player moves the cursor somewhere else, the card changes the material tint back to normal.

Problem

I want the card to turn to the front side up by clicking on it so I used the OnMouseDown() method in Unity. This is currently my code in the Update() method:

 void Update()
     {
         if (startCheck)
         {
             transform.rotation = Quaternion.RotateTowards(transform.rotation, beginRotation, flipSpeed * Time.deltaTime); //used for start sequence
         }
 
         if (mouseEnter)
         {
             Transform child = gameObject.transform.FindChild("CardBack");
             child.GetComponent<Renderer>().material.color = Color.Lerp(child.GetComponent<Renderer>().material.color, Color.red, hoverOverSpeed * Time.deltaTime);
             if (clicked)
             {
                 transform.rotation = Quaternion.RotateTowards(transform.rotation, beginRotation, flipSpeed * Time.deltaTime);
             }
         }
         else
         {
             Transform child = gameObject.transform.FindChild("CardBack");
             child.GetComponent<Renderer>().material.color = Color.Lerp(child.GetComponent<Renderer>().material.color, Color.white, hoverOverSpeed * Time.deltaTime);
         }
     }

However, whenever I press a card after the starting rotation, nothing happens. I have tried multiple other ways even by trying to make the rotation immediate by using for example transform.Rotate but still, nothing worked. But, I know it is something I'm doing wrong because I did a Debug.Log in the 'if (clicked)' statement instead of the attempt of rotating the card and the Debug.Log worked. Can anyone tell me what I am doing wrong and how to fix it? Also, I only recently delved into Unity like this so I appreciate if people simplify things for me too. :)

P.S. I also hope I posted this correctly since this is my first question so sorry if I did this wrong!

Key

  • startCheck is a boolean which is only set to true after the delay. This causes the cards to do the flip animation in the beginning (after the delay).

  • beginRotation is a Quaternion used to make the cards flip properly by making them turn Vector3.forward for 180 degrees.

  • flipSpeed is self-explanatory.

  • mouseEnter is a boolean which is set to true after the user hovers the cursor over a card and is set to false whenever he moves the cursor somewhere away from the card.

  • clicked is a boolean which is set to true after the player presses on a card.

Comment
Add comment · Show 7
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 hexagonius · Dec 11, 2016 at 09:23 AM 0
Share

just to clarify, beginRotation is used to turn the card upside down, or up?

avatar image andracer108 hexagonius · Dec 11, 2016 at 09:52 AM 0
Share

@hexagonius beginRotation is used to make the cards flip with their back side up. How the game works is that in the beginning, the scene shows the cards to the player what their pattern/image is and then after a delay of 2 seconds, the cards flip over to show their back side. To show their back side, the cards use the beginRotation Quaternion. However, I used it again in the if (clicked) statement just to make it turn 180 degrees again. Could that be what's wrong?

avatar image hexagonius andracer108 · Dec 11, 2016 at 10:29 AM 0
Share

ok, then that's the problem. a Quaternion is an orientation in space. like the Quaternion.identity always is "look forward with your up vector up". right now your code tries to rotate the cards face down where they are already.
either create another Quaternion with a 180 degree opposite from your beginRotation, which might just be Quaternion.Identity, or save the start rotation of the cards at the beginning when they face up and use that

Show more comments
avatar image UnityCoach · Dec 11, 2016 at 03:09 PM 0
Share

I would use animations with an Animator Controller for this kind of thing. You would create animation states with a condition based on a boolean parameter. Then you simply have to change that boolean parameter via the code. Simple, and very flexible.

avatar image andracer108 UnityCoach · Dec 11, 2016 at 04:06 PM 0
Share

@jikkou The only reason I wouldn't use that method is because we haven't learned how to use it at school and I don't want to delve too much into making this. If possible, all I want is a way on how I could fix this problem by using a Quaternion method. So if you know a way, please do inform me.

avatar image UnityCoach andracer108 · Dec 11, 2016 at 08:30 PM 0
Share

Hum, I personally don't like those implementations, sometimes given in the docs, where you use the current state as a start state. It gives a damping effect, which can be nice, but over which you have no control.

Usually, I prefer to do interpolations between two static states, like :

     private bool clicked;
     private float slerpAmount = 0;
     private Quaternion slerpStart;
     private Quaternion slerpEnd = Quaternion.identity;
 
     void On$$anonymous$$ouseUpAsButton ()
     {
         clicked = true;
         slerpStart = transform.rotation;
     }
 
     void Update()
     {
         if (clicked)
         {
             slerpAmount += Time.deltaTime;
             if (slerpAmount <= 1)
             {
                 transform.rotation = Quaternion.Slerp(slerpStart, slerpEnd, slerpAmount);
             }
             else
             {
                 slerpAmount = 0;
                 clicked = false;
             }
         }
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by andracer108 · Dec 12, 2016 at 10:38 AM

@jikkou Thank you, I'll try this out whenever I have the chance and will get back to you with the result. :)

EDIT: I have fixed the problem by using Lerp and by leaving beginRotation as is and adding a chosenRotation which is set to Quaternion.identity.

Here's the code used in the if (clicked) statement:

 if (clicked)
         {
             transform.rotation = Quaternion.Lerp(transform.rotation, chosenRotation, 5 * (currentTurnTime / 2)); //choosing a card
         }

currentTurnTime is a variable which is found in Update() and is set to Time.deltaTime and is also reset to 0 whenever OnMouseUpAsButton() is activated.

Thank you all for the help! :)

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

Using quaternions to align game objects. 1 Answer

Know when the object is rotated? 1 Answer

How to set y rotation of an object to be z rotation of another? 1 Answer

How do I rotate the GameObejcts smoothly with negative degrees 0 Answers

Flip 3D Character Rotation 180 on Y axis 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