Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Apr 25, 2014 at 10:12 AM by Calum-McManus for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Calum-McManus · Oct 18, 2013 at 03:02 PM · c#rotationflipover time

Flip over an object (smooth transition)

Simple enough, lift the card off the table flip it over 180 degrees and place it back down, by clicking on it. I can do the raycasts on click but i just can't think of how to flip it over. i was thinking a while loop in a coroutine? help :3

Edit:

So I do not want to use animations I spent some time and come up with the rotation code and just need now to make it move up a bit after that.

 using UnityEngine;
 using System.Collections;
 
 public class FlipCard : MonoBehaviour {
     
     private bool m_CanFlip = true;
     private bool m_Match = false;
         
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 if(Physics.Raycast(ray, out hit))
                 {
                 Debug.Log(tag);
                     if(hit.collider.CompareTag("Card"))
                     {
                         StartCoroutine(CardFlip());
                     }
                 }
         }
     }
                 private IEnumerator CardFlip()
             {
         
         Quaternion angle = Quaternion.Euler(0, -180, 0); Quaternion reverce = Quaternion.Euler(0, 0, 0); 
         
         if(m_CanFlip == true)
         {
             
         while(Quaternion.Angle(transform.rotation, angle)>5)
                     {
                         transform.rotation = Quaternion.Slerp(transform.rotation, angle, Time.deltaTime * 5);
             yield return new WaitForEndOfFrame();
                     }
         transform.rotation = angle;
         m_CanFlip = false;
             
             }
         if(m_Match == false)
             {
                 yield return new WaitForSeconds(1.5f);
                 while(Quaternion.Angle(transform.rotation, reverce)>5)
                     {
                         transform.rotation = Quaternion.Slerp(transform.rotation, reverce, Time.deltaTime * 5);
             yield return new WaitForEndOfFrame();
                     }    
                 transform.rotation = reverce;
                 m_CanFlip = true;
             }
     }
 }
 

So what this does is on click it will flip the card and then after some time it flip back over is the Bool m_Match is false (as later there will be more cards and I will compare there tags to see if they match) but now I need this to also raise the card very slightly on click before it flips. Thanks!

Comment
Add comment · Show 3
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 robertbu · Oct 18, 2013 at 04:19 PM 0
Share

There are many posts on smooth movement and smooth rotation. You might start with this script in the Wiki:

http://wiki.unity3d.com/index.php?title=$$anonymous$$oveObject

avatar image zombience · Oct 18, 2013 at 04:40 PM 0
Share

also, try using the animation system. if the card should always perform the same action and doesn't need to be generative or react to the environment while it's flipping over, animations are your simple go-to for this sort of thing.

avatar image Calum-McManus · Oct 22, 2013 at 12:48 PM 0
Share

Im looking at using Quaternion in a while loop using a co-routine?

3 Replies

  • Sort: 
avatar image
0

Answer by zombience · Oct 22, 2013 at 04:48 PM

If you are committed to using strict code and staying away from animations, here is a simple script that will lift an object up and put it back down in place, rotated 180.

I maintain that, from what I can see, it seems like animations would be your best bet.

Place the following code on an object. in the inspector, drag the "progress" value up and watch as your object lifts up, rotates, and moves back down.

 using UnityEngine;
 using System.Collections;
 
 public class Flip : MonoBehaviour 
 {
 
     public float progress = 0;
     public float height = 5f;
 
     private Vector3 origin;
     private Vector3 origRot;
     private Vector3 flipRot;
 
     void Start () 
     {
         origin = transform.position;
         flipRot = origRot = transform.rotation.eulerAngles;
         flipRot.z += 180;
     }
 
     void Update () 
     {
         progress = Mathf.Clamp(progress, 0, 90);
 
         Vector3 pos = origin;
         pos.y = (Mathf.Sin(Mathf.Deg2Rad * progress) * Mathf.Cos(Mathf.Deg2Rad * progress) * height) + origin.y;
 
         transform.position = pos; // handle moving upwards
         transform.rotation = Quaternion.Lerp(Quaternion.Euler(origRot), Quaternion.Euler(flipRot), progress / 90); // handle rotation
 
     }
 }
 
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
avatar image
-1

Answer by Waffle842 · Oct 18, 2013 at 06:04 PM

have you tried a slerp or a lerp?

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

Answer by haim96 · Oct 22, 2013 at 01:04 PM

i had similar issue with flipping my airplane. i ended up with creating animation clip and play it when it needed. it works really great because i could add realistic flipping movement.

make sure you change animation type to legacy in the imported mesh (if any) create new animation clip, adjust the curves you need and play the animation with code.

check this for more info: http://docs.unity3d.com/Documentation/Manual/AnimationEditorGuide40.html

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

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Multiple Cars not working 1 Answer

Rotate a Canvas element over time 0 Answers

Distribute terrain in zones 3 Answers

accessing an array of rigidbodies at once? c# 2 Answers

Velocity bug with hinge joints forces in C# 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