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 Lynkfox · Feb 25, 2019 at 05:36 AM · quaternionrotatetransform.rotation

Rotating by 90 degrees eventually locks

Back again. Currently using a coroutine to rotate a level around a pivot point (It is the child of the pivot point, so I am rotating the point and that makes everything work well)

Two problems. After 7 clockwise rotations, Quaternion.Dot(level.rotation, toangle) never reaches 1.

and counterclockwise rotations just bounce back and forth between the first move and back to the original position.

Here is my coroutine:

     private IEnumerator SpinLevel(Vector3 direction)
     {
 
         player.transform.SetParent(this.transform); // adds the player object to the level, so it will rotate with the level and not get tossed out
         toAngle = Quaternion.AngleAxis(startDegree + rotationDegree, direction);
 
         while (Quaternion.Dot(level.rotation, toAngle) < 1.0f)
         {
             transform.rotation = Quaternion.RotateTowards(transform.rotation, toAngle, rotateSpeed*Time.deltaTime);
 
             yield return null;
         }
         yield return new WaitForEndOfFrame();
 
         startAngle = transform.rotation;
         levelRotating = false;
         rotatingCounterClock = false;
         rotatingClockwise = false;
         player.transform.parent = null;
     }


and here is the rest

    [SerializeField] private float rotationDegree = 90f; // how far you want the level to rotate. May need to be made public in order to change it for different levels.
     [SerializeField] private float rotateSpeed = 100f; // speed of the rotation
     [SerializeField] GameObject player;
 
     // public flags for use in other scripts. Other objects will need to know if the world is moving clock or counter clockwise, or even if it is moving
     private bool rotatingClockwise = false, rotatingCounterClock = false, levelRotating = false;
 
     private Quaternion toAngle, startAngle;
     private float startDegree;
     private Transform level;
 
 
     private void Awake()
     {
  
         level = this.transform; //shorthand, chached.
         startAngle = Quaternion.identity; // Make sure the level startAngle is set to No Rotation to start.
     }
     private void FixedUpdate()
     {
         if (!levelRotating)
         {
             startDegree = startAngle.eulerAngles.z;
             
             
 
             if (Input.GetAxisRaw("Rotate") > 0)
             {
                 //clockwise rotation via the e key
                 rotatingClockwise = true; // set the direction flag
                 levelRotating = true;
                 StartCoroutine(SpinLevel(-level.forward));
             }
             else if (Input.GetAxisRaw("Rotate") < 0)
             {
                 //counterclockwise rotation via the q key
                 rotatingCounterClock = true;
                 levelRotating = true;
                 StartCoroutine(SpinLevel(level.forward));
             }
             
         } //else levelRotationg = true;

     }


I had thought that using Quaternions was supposed to prevent the issue of getting locked... I do realize that Quaternion.Dot compares two floats and finds if they are 'close enough' - when I put a debug in the while loop, at the lock point Quaternion.dot just returns .99999 - (and I assume repeating) and never gts close enough for Dot to say they are equal.

I tried setting the transform after each rotation to hard set the eulerAngle - but that seems to defeat the purpose of using Quaternions anyways, and it didn't work.

I also tried using !MathF.Approximately(dot,1) rather than Dot < 1 but it still runs into the same issue (i assume because Dot uses something akin to Approximately)

and I have - no idea why the counterclockwise is bouncing between the two. In previous incarnations of this script, using vector3.forward and -vector3.forward was an easy way to move clock and counterclockwise without having to change the start/end angles. I don't know why it stopped working when I went to an While statement. (maybe it was only seeming to work in the first place)

Any help would be appreciated. I've been banging my head against this level rotation script all day.

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

2 Replies

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

Answer by troien · Feb 25, 2019 at 04:47 PM

As far as the "it is close to 1, but not exactly", did you try using the equals operator instead?

If you read the documentation you'll see that it returs true if 'close to' 1, and Unity probably implemented that differently then you did. (You can use != awell, but Unity hasen't got that one documented in the docs, so can't link to that)

 while (level.rotation != toAngle)

As for the counter clockwise part. That is probably because of how you calculate the angle, Using 'startdegree' like that is not going to work in all cases, as when you start rotating, euler angles can flip halfway through a rotation because there can be more ways to display the same rotation in eulerangles, Unity is going to pick the one that is it's quaternion to euler conversion function has implemented, which might not be the one you expect.

Therefore, as input for your new rotation, don't just use the z axis of the rotation, use the entire rotation, like so:

 toAngle = transform.rotation * Quaternion.AngleAxis(rotationDegree, direction);

.

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 Lynkfox · Feb 25, 2019 at 05:00 PM 0
Share

Thank you!

I did try the == (and !=) issues, but recieved the exact same problem.

And I had figured out the second problem, but not a solution to it. That makes muchmore sense!

I have sense moved the code out of CoRoutine and into FixedUpdate with the rest of the control code, now that I have some sort of while fix. Still running into some issues, but getting much cleaner now.

I'll submit this as an answer to close this thread, because the problem has now changed, and what you have stated will work well for people googling it, I hope.

avatar image Lynkfox Lynkfox · Feb 25, 2019 at 05:08 PM 0
Share

just FYI - oh my gooodnes. I can't believe it. just forgetting that tranform.rotation*quat aspect was what was causing 90% of my bugs. That has fixed most of my problems, even in my new code out of a coroutine. Thank you!

avatar image
0

Answer by Lynkfox · Feb 25, 2019 at 08:02 PM

For anyone googling that comes across this in an attempt to make their own version, here is the final code I used: I did indeed switch to !=Angle, but instead of using RotateTowards or transform.rotate, I switched to Quaternion.Lerp. This removed the jerkyness when correcting the final position, and also seemed to prevent the floating point error with Quaternion.Dot

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RotateController : MonoBehaviour
 {
     [SerializeField] private float rotationDegree = 90f; // how far you want the level to rotate. May need to be made public in order to change it for different levels.
     [SerializeField] private float rotateSpeed = 5f; // speed of the rotation
     [SerializeField] GameObject player;
 
 
 
     private Quaternion toAngle;
     private Transform level;
     private int rotateCount = 0;
 
     private void Awake()
     {
  
         level = this.transform; 
 
         startAngle = Quaternion.identity; 

     }
     private void FixedUpdate()
     {
         if (!levelRotating)
         {
             
             if (Input.GetAxisRaw("Rotate") > 0)
             {

                 levelRotating = true;
                 rotateCount--;
                 toAngle = transform.rotation * Quaternion.AngleAxis(rotationDegree, -transform.forward);
 
             }
             else if (Input.GetAxisRaw("Rotate") < 0)
             {

                 levelRotating = true;
                 rotateCount++;
                 toAngle = transform.rotation * Quaternion.AngleAxis(rotationDegree, transform.forward);
             }
 
         }
         else // levelRotationg = true;
         {
 
 
             /* The actual rotate. This figures out Clockwise and Counterclockwise by switching the axis of the rotate. Forward rotates counter clockwise (Left Handed Cordinate System
              * means that X -> Y.) -Forward will routate clockwise. This is done in the toAngle calculation above.
              */

             if(transform.rotation != toAngle)   
             {
                 player.transform.SetParent(this.transform); 

                 transform.rotation = Quaternion.Lerp(transform.rotation, toAngle, rotateSpeed / 100); 


             }
             else //Cleanup
             {
                 
                 if( rotateCount < 360/rotationDegree)
                 {
 
                     transform.rotation = toAngle;
 
                     
                 } else //destination rotate count is greater than or equal to the amount of times the degRotate can divide into a circle)
                 {
                 
                     transform.rotation = Quaternion.identity; 
                     rotateCount = 0;
                     /* Note:
                      * 
                      * Currently no Error checking if rotationDegree is not a whole divisor of 360. Error checking will be needed in awake
                      */
                 }

                 levelRotating = false;
                 player.transform.parent = null;
             }
         }
         
     }
     
 }
 
 




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

103 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

Related Questions

Quaternion.identity giving outlier result? 1 Answer

Store object's starting position and rotation and have it return there. 2 Answers

Rotate Gameobject by vertex shader with quaternion 0 Answers

Doesn't Add Rotation Over 180 2 Answers

rotate player on y axis or Vector.up regradless of orientation 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