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
1
Question by galtay · Mar 31, 2016 at 07:52 AM · c#rotationclicksmooth

Smooth rotation of cube on mouse click with no interruption, improve?

Hi all, I'm learning Unity and working on a simple tile/cube clicking game. I'd like a smooth 180 degree rotation upon clicking a cube. I also don't want the player to be able to initiate another rotation while one is in progress. I cobbled together a script from some pieces I found but it looks kind of clunky. Does anyone see some places for improvement? I'm not sure if checking the angle difference constantly to tell when the rotation is done is the best way to do this. Any suggestions welcome. Code below,

 public class TileController : MonoBehaviour {
 
     public float rotTime = 0.1f;
     public float rotationMargin = 0.01f;
 
     private Vector3 targetAngle;
     private bool isRotating = false;
     private float angleDiff;
 
     void OnMouseDown()
     {
         // set a new target angle if we are not rotating 
         if (!isRotating) {
             targetAngle = new Vector3 (
                 transform.eulerAngles.x, 
                 transform.eulerAngles.y + 180.0f, 
                 transform.eulerAngles.z);
             isRotating = true;
         }    
     }
 
 
     // Update is called once per frame
     void Update () {
         
         if (isRotating) {
             float yVelocity = 0.0f;
             float yAngle = Mathf.SmoothDampAngle (
                                transform.eulerAngles.y, 
                                targetAngle.y,
                                ref yVelocity,
                                rotTime);
             
             transform.eulerAngles = new Vector3 (
                 transform.eulerAngles.x,
                 yAngle,
                 transform.eulerAngles.z);
             
             angleDiff = Mathf.Abs( Mathf.DeltaAngle (yAngle, targetAngle.y));
             isRotating = angleDiff > rotationMargin;
         } 
             
     }
 }

Comment
Add comment · Show 4
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 ZefanS · Mar 31, 2016 at 08:21 AM 0
Share

You could look into using Linear Interpolation for the rotation. This would eli$$anonymous$$ate the need for the check and can make the rotation smooth.

Something to get you started

avatar image galtay ZefanS · Apr 01, 2016 at 12:25 AM 0
Share

Thanks. After reading your link and some google searching I was lead to this post, http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly I will post an updated script in the answer section which is a modified version of the script from the link above. :)

avatar image ZefanS galtay · Apr 01, 2016 at 12:32 AM 0
Share

Glad it was helpful. Also, that link is really informative. Nice find!

Show more comments

1 Reply

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

Answer by galtay · Apr 01, 2016 at 12:27 AM

I modified the script in this post (http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly) to achieve the rotation I was after ...

 public class SlerpRot : MonoBehaviour {
 
     /// The time taken to complete transform
     public float timeTakenDuringSlerp = 1f;
 
     // The angle to rotate through in degrees
     public float angularChange = 180;
 
     // Whether we are currently interpolating or not
     private bool _isSlerping;
 
     //The start and finish Quaternions for the interpolation
     private Quaternion _startPosition;
     private Quaternion _endPosition;
     private Vector3 _tmpEuler;
 
     //The Time.time value when we started the interpolation
     private float _timeStartedSlerping;
 
 
     // Called to begin the linear interpolation
     void StartSlerping()
     {
         _isSlerping = true;
         _timeStartedSlerping = Time.time;
 
         //We set the start position to the current rotation, and the finish to angularChange around the y-axis
         _startPosition = transform.rotation;
         _tmpEuler = new Vector3(
             _startPosition.eulerAngles.x,
             _startPosition.eulerAngles.y + angularChange,
             _startPosition.eulerAngles.z);
         _endPosition = Quaternion.Euler (_tmpEuler);
     }
 
     void OnMouseDown()
     {
         if (!_isSlerping) {
             StartSlerping ();
         }
     }
 
 
     //We do the actual interpolation in FixedUpdate()
     void FixedUpdate()
     {
         if(_isSlerping)
         {
             //We want percentage = 0.0 when Time.time = _timeStartedSlerping
             //and percentage = 1.0 when Time.time = _timeStartedSlerping + timeTakenDuringSlerp
             //In other words, we want to know what percentage of "timeTakenDuringSlerp" the value
             //"Time.time - _timeStartedSlerping" is.
             float timeSinceStarted = Time.time - _timeStartedSlerping;
             float percentageComplete = timeSinceStarted / timeTakenDuringSlerp;
 
             //Perform the actual slerping.  Notice that the first two parameters will always be the same
             //throughout a single slerp-processs (ie. they won't change until we click the mouse again
             //to start another slerp)
             transform.rotation = Quaternion.Slerp (_startPosition, _endPosition, percentageComplete);
 
             //When we've completed the slerp, we set _isSlerping to false
             if(percentageComplete >= 1.0f)
             {
                 _isSlerping = false;
             }
         }
     }
 
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why isn't my object lerping ? C# 2 Answers

How to make a smooth rotation? 1 Answer

Instantiated Object Has Wrong Rotation 0 Answers

Freeze rotation X but rotation Z, how can I do this? 0 Answers

Struggling to get the rotation the player is moving in. 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