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 /
avatar image
0
Question by Dewlshock · Jun 20, 2018 at 07:21 AM · rotationrotatecoroutine

Rotate an object 180 degrees smoothly in a particular direction

My current code achieves the result I want but is horribly inefficient and bug prone. I'm hoping someone can explain to me how to get an object to smoothly flip in a particular direction depending on the current direction value.

 public void OnTriggerExit () {

     if (!isRunning){
         //get the direction to flip in
         currentPlayer = FindCurrentPlayer ().GetComponent<PlayerControl> ();
         flipDirection = currentPlayer.direction;
         //start flip coroutine
         isRunning = true;
         StartCoroutine(Flip());
     }
 }

 // Flip Tile
 IEnumerator Flip(){

     if (flipDirection == "up")
     {
         transform.Rotate(flipSpeed,0,0, Space.World);
         while (Flipping())
         {
             transform.Rotate(flipSpeed,0,0, Space.World);
             yield return null;
         }
     }
     if (flipDirection == "left")
     {
         transform.Rotate(0,0,flipSpeed, Space.World);
         while (Flipping())
         {
             transform.Rotate(0,0,flipSpeed, Space.World);
             yield return null;
         }
     }
     if (flipDirection == "down")
     {
         transform.Rotate(-flipSpeed,0,0, Space.World);
         while (Flipping())
         {
             transform.Rotate(-flipSpeed,0,0, Space.World);
             yield return null;
         }
     }
     if (flipDirection == "right")
     {
         transform.Rotate(0,0,-flipSpeed, Space.World);
         while (Flipping())
         {
             transform.Rotate(0,0,-flipSpeed, Space.World);
             yield return null;
         }
     }
 }

 //Check if tile is still flipping (hasn't reached a point where it can stop yet)
 bool Flipping() {

     if (transform.rotation.x != 1 && transform.rotation.x != -1 && transform.rotation.y != 1 && transform.rotation.y != -1 && transform.rotation.z != 1 && transform.rotation.z != -1 && transform.rotation.w != 1 && transform.rotation.w != -1)
     {
         return true;
     }
     else{
         isRunning = false;
         occupied = false;
         return false;
     }
 }
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
Best Answer

Answer by Harinezumi · Jun 20, 2018 at 07:57 AM

The code isn't that bad, but it can be greatly simplified by creating helper variables, for example for the rotation. A different problem is that the code rotates independently from the frame rate, so it can look jerky. It is better to specify the duration of the flipping, and use Quaternion.Lerp() or Slerp() to rotate (see usage below).
Also note that in this form the OnTriggerExit() won't be called when exiting a trigger, because it doesn't match the the Unity callback function's signature, which has a Collider parameter.
Finally, it would be a lot more efficient to define an enum for the flipDirection instead of using a string.
Now some code:

 public enum FlipDirection { UP, DOWN, LEFT, RIGHT }; // change type of flipDirection in PlayerControl to FlipDirection
 
 private Coroutine flipCoroutine;
 
 public void OnTriggerExit (Collider other) {
     if (flipCoroutine != null) { return; }
     PlayerControl playerControl = other.GetComponent<PlayerControl>();
     if (playerControl != null) { flipCoroutine = StartCoroutine(Flip(playerControl.direction); }
 }
 
 private IEnumerator Flip (FlipDirection flipDirection) {
     Quaternion startRotation = transform.rotation;
     Quaternion targetRotation;    
     switch (flipDirection) {
         case FlipDirection.UP: targetRotation = Quaternion.Euler(90, 0, 0); break;
         case FlipDirection.DOWN: targetRotation = Quaternion.Euler(-90, 0, 0); break;
         case FlipDirection.LEFT: targetRotation = Quaternion.Euler(0, 0, 90); break;
         case FlipDirection.RIGHT: targetRotation = Quaternion.Euler(0, 0, -90); break;
         default: flipCoroutine = null; yield break; // unexpected case, stop coroutine
     }
 
     float t = 0;
     while (t < flipDuration) {
         t += Time.deltaTime;
         transform.rotation = Quaternion.Slerp(startRotation, targetRotation, t / flipDuration);
         yield return null;
     }
     transform.rotation = Quaternion.Slerp(startRotation, targetRotation, 1); // make sure the flipping completes precisely
     flipCoroutine = null; // mark the flipping finished
 }

Note, that the above code arrives from any starting rotation to the target rotation over the same time. If the object does not start out in an exactly opposite direction, it will rotate slower. However, I don't know of an equivalent of InverseLerp() for rotations so you might need to find a solution for this if this is a problem.

Comment
Add comment · Show 3 · 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 Dewlshock · Jun 25, 2018 at 03:31 AM 0
Share

Thanks for the answer. Unfortunately because I am wanting to flip these objects exactly 180 degrees this method ends up with the tiles rotating in any number of directions from the startRotation to the targetRotation. Due to the nature of Lerp and Slerp I am unable to designate a particular direction for the objects to rotate in.

Any other ideas?

avatar image Harinezumi Dewlshock · Jun 25, 2018 at 08:01 AM 0
Share

I see, that wasn't clear from your description, but now it makes sense why you wrote the code the way you did. Fortunately, I don't think it will be difficult to do what you want to achieve.

First, if you haven't yet, try Quaternion.Lerp() ins$$anonymous$$d of Slerp(), because Slerp() tries to find the shortest path, while Lerp() doesn't.
If that doesn't work, replace the Flip() function with the following code:

 private IEnumerator Flip (FlipDirection flipDirection) {
     Quaternion startRotation = transform.rotation;
     Quaternion targetRotation;
     Quaternion rotation;
     switch (flipDirection) {
         case FlipDirection.UP:
             rotation = Quaternion.Euler(90 / flipDuration, 0, 0);
             targetRotation = Quaternion.Euler(90, 0, 0);
             break;
         case FlipDIrection.DOWN:
             rotation = Quaternion.Euler(-90 / flipDuration, 0, 0);
             targetRotation = Quaternion.Euler(-90, 0, 0);
             break;
         case FlipDirection.LEFT:
             rotation = Quaternion.Euler(0, 0, 90 / flipDuration);
             targetRotation = Quaternion.Euler(0, 0, 90);
             break;
         case FlipDirection.RIGHT:
             rotation = Quaternion.Euler(0, 0, -90 / flipDuration);
             targetRotation = Quaternion.Euler(0, 0, -90);
             break;
         default: flipCoroutine = null; yield break; // unexpected case, stop coroutine
     }
 
     float t = 0;
     while (t < flipDuration) {
         t += Time.deltaTime;
         transform.rotation *= rotation;
         yield return null;
     }
     transform.rotation = targetRotation; // make sure the flipping completes precisely
     flipCoroutine = null; // mark the flipping finished
 }

Note that this will rotate too much if the flipping doesn't start at a 90 degree angle. To compensate for that, you should check if the starting angle has rotation along the axis you will rotate, and subtract it from the desired angle (e.g. (90 - startAngle) / flipDuration)). But this requires some experimenting, and I don't have the time for that at the moment. Sorry.

avatar image Dewlshock Harinezumi · Jun 26, 2018 at 01:09 AM 1
Share

Not a problem. I'll experiment with these methods and see what I can get happening. Thanks again for your time, much appreciated.

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

126 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

Related Questions

How to rotate an object around another 60 degrees with a keypress? 0 Answers

How to slowly rotate a gameObject from 0 to -90f and vice versa every 5 seconds? 1 Answer

Coroutine Rotation Drifting/Imprecise 1 Answer

gameobject stops moving correctly when rotating 1 Answer

void update working under conditions 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