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
1
Question by Richard Slater · Oct 24, 2010 at 08:47 PM · rotationslerpflipspin

Smoothly flipping an object on one axis on click then back again

I have been working on a way to display an object in front of the camera that could be flipped when it was clicked, the immediate application is to provide a way to flip a "card" over to reveal the reverse site.

To an extent I have got this working.

There is an issue with this however in that once it has done the initial flip from the front side to the reverse the flip back is instant.

public class SpinObjectOnClick : MonoBehaviour {

 private enum CardSide
 {
     Front,
     Back
 }

 private bool rotating = false;
 private CardSide side = CardSide.Front;
 private Quaternion initialRotation;
 private Quaternion targetRotation;

 public void OnMouseUp()
 {
     if (rotating)
         return;

     rotating = true;

     if (side == CardSide.Front)
     {
         initialRotation = transform.rotation;
         targetRotation = Quaternion.AngleAxis(0, Vector3.right) * Quaternion.AngleAxis(0, Vector3.up) * Quaternion.AngleAxis(0, Vector3.forward);
     }
     else
     {
         targetRotation =  initialRotation;
     }
 }

 public void Update()
 {
     if (!rotating)
         return;

     Debug.Log("Rotating " + transform.rotation.y + " End = " + targetRotation);

     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 4);

     // consider the card flipped over when it is so close there is no perceptable reason why not
     if (transform.rotation.y - targetRotation.y < 0.001)
     {
         rotating = false;
         side = side == CardSide.Front ? CardSide.Back : CardSide.Front;
         transform.rotation = targetRotation ;
     }
 }

}

I am happy to accept any other comments about the code as well, this is only Day 4 of my Unity3d journey.

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
1
Best Answer

Answer by BinaryCaveman · Oct 24, 2010 at 09:16 PM

I think the issue is in this line:

 if (transform.rotation.y - targetRotation.y < 0.001)

which conflicts with this line:

 transform.rotation = targetRotation;

The difference of the rotation from when the object is rotated backwards to when the object is rotated forwards is negative. Using the if statement you wrote, it considers transform.rotation and targetRotation to be near, and it sets the rotation of the object to targetRotation.

You want to change your code to this:

 // Use the absolute value to change the difference to positive before checking
 if (Mathf.abs(transform.rotation.y - targetRotation.y) < 0.001)
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 Richard Slater · Oct 25, 2010 at 12:54 PM 0
Share

That was the ticket, thanks.

avatar image BinaryCaveman · Oct 25, 2010 at 11:27 PM 0
Share

You're welcome! :D

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

No one has followed this question yet.

Related Questions

Flip over an object (smooth transition) 3 Answers

Slerp camera rotation moves on it's own at runtime? 1 Answer

need my object to rotate between specific angles on the x axis 2 Answers

Rotation of an object with quaternion 1 Answer

Spinning Sphere/Globe IOS 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