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
0
Question by Lynkfox · Feb 24, 2019 at 10:03 PM · mathfrotatearoundwhile

Rotate around a pivot to a specified degree and stop, with key press

UnityServicesMade with UnityLearnCommunityAsset StoreGet Unity

ForumsAnswersFeedbackIssue TrackerBlogEvangelistsUser Groups Find posts and topics... Ask a question
Spaces

Ask a Question The form below has errors. Correct the fields in red and try again. avatar image Your Question: rotating an object around a pivot inside and stopping at a set degree

I've been struggling with this one all morning. I have a game I'm designing where the entire level can rotate on a set pivot. I am rotating the level because, not the camera, because gravity needs to remain unchanged. My script, when Q or E is hit, rotates the entire level. (Currently in the X/Y plane, rotating around the z)

I was able to get it working without a pivot (it would rotate around 0,0,0) In the interest of being able to have multiple rotation points as levels get bigger, i was adjusting my script. I am having trouble getting it to stop - I have tried a bunch of different ways: my current version is setting a vector at an angle equal to the desired rotation and then using vector3.dot to see if they line up as the script rotates it.

The rotation is also in a coroutine.

If you could please help me figure out how to get this while loop to recognize that the vectors have aligned, I woudl be very grateful

      [SerializeField] public GameObject level; // levels will change.
     [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 = 15f; // speed of the rotation
     
 private bool levelRotating = false,
 
 Quaternion toAngle, startAngle;
 private float startDegree;
 
 private Transform levelTrans;
     private Transform pivot;
 
     private void Awake()
     {
         levelTrans = level.transform; // shorthand for the transform of the level vrs the GO
 
         startAngle = Quaternion.identity; // Make sure the level startAngle is set to No Rotation to start.
 
         foreach(Transform child in levelTrans) 
         {
             // searches for each child in the Level for the pivot point on layer 9 (pivot layer) and then sets it for use later. Should only be one pivot per camera move.
 
             // Note: Camera Move script (to change levels/scenes) will need to re find pivot!!
             if(child.gameObject.layer == 9)
             {
                 pivot = child;
                 break;
             }
         }
         
     }
 
 
    private void LateUpdate()
     {
         if (!levelRotating)
         {
 
             startDegree = levelTrans.eulerAngles.z;
 
             if (Input.GetAxisRaw("Rotate") < 0)
             {
 
                 toAngle = Quaternion.AngleAxis(startDegree + rotationDegree, levelTrans.forward); // set the destination angle as an offset from the start angle
 
                 this.transform.SetParent(levelTrans); // adds the player object to the level, so it will rotate with the level and not get tossed out
                 rotatingClockwise = true; // set the direction flag
                 levelRotating = true; 
                 StartCoroutine(SpinLevel(rotateSpeed, Vector3.forward, pivot));
                 
             }
             else if (Input.GetAxisRaw("Rotate") > 0)
             {
                 toAngle = Quaternion.AngleAxis(startDegree + rotationDegree, -levelTrans.forward); 
                 this.transform.SetParent(levelTrans);
                 rotatingCounterClock = true;
                 levelRotating = true; 
                 StartCoroutine(SpinLevel(rotateSpeed, -Vector3.forward, pivot));
                 
             }
         } // else do nothing if the level is moving. No change in current location, no change in flags, no change in toAngle
     }
 
 
  private IEnumerator SpinLevel(float overTime, Vector3 rotDirection, Transform pivot)
     {
 
 
         Vector3 pivotPoint = pivot.position;
 
         Vector3 destination = FindEndRotation(rotationDegree);
         Vector3 start = startAngle.eulerAngles;
 
        
 
         float dotRotateCheck = 0.0f;
 
 
         // if they are different, rotate the level a small amount, and set the level rotating to True
 
         while (!Mathf.Approximately(dotRotateCheck,0.0f))
         {
                 
             levelRotating = true;
 
             levelTrans.RotateAround(pivotPoint, rotDirection, overTime * Time.deltaTime);
 
             dotRotateCheck = Vector3.Dot(levelTrans.transform.right, destination);
 
 
 
             yield return null; 
         }
 
 
             levelRotating = false;
 
             this.transform.parent = null;
             
         
     }
 
 
     private Vector3 FindEndRotation(float degree)
     {
         Vector3 end = new Vector3(0,0,0);
 
         float radians = degree * Mathf.Deg2Rad;
 
         end.x = Mathf.Cos(radians);
         end.y = Mathf.Sin(radians);
 
         
         return end;
     }



I've been struggling with this one all morning. I have a game I'm designing where the entire level can rotate on a set pivot. I am rotating the level because, not the camera, because gravity needs to remain unchanged. My script, when Q or E is hit, rotates the entire level. (Currently in the X/Y plane, rotating around the z)

I was able to get it working without a pivot (it would rotate around 0,0,0) In the interest of being able to have multiple rotation points as levels get bigger, i was adjusting my script. I am having trouble getting it to stop - I have tried a bunch of different ways: my current version is setting a vector at an angle equal to the desired rotation and then using vector3.dot to see if they line up as the script rotates it.

The rotation is also in a coroutine.

If you could please help me figure out how to get this while loop to recognize that the vectors have aligned, I woudl be very grateful

   [SerializeField] public GameObject level; // levels will change.
  [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 = 15f; // speed of the rotation
  

private bool levelRotating = false,

Quaternion toAngle, startAngle; private float startDegree;

private Transform levelTrans; private Transform pivot;

  private void Awake()
  {
      levelTrans = level.transform; // shorthand for the transform of the level vrs the GO
 
      startAngle = Quaternion.identity; // Make sure the level startAngle is set to No Rotation to start.
 
      foreach(Transform child in levelTrans) 
      {
          // searches for each child in the Level for the pivot point on layer 9 (pivot layer) and then sets it for use later. Should only be one pivot per camera move.
 
          // Note: Camera Move script (to change levels/scenes) will need to re find pivot!!
          if(child.gameObject.layer == 9)
          {
              pivot = child;
              break;
          }
      }
      
  }
 
 
 private void LateUpdate()
  {
      if (!levelRotating)
      {
 
          startDegree = levelTrans.eulerAngles.z;
 
          if (Input.GetAxisRaw("Rotate") < 0)
          {
 
              toAngle = Quaternion.AngleAxis(startDegree + rotationDegree, levelTrans.forward); // set the destination angle as an offset from the start angle
 
              this.transform.SetParent(levelTrans); // adds the player object to the level, so it will rotate with the level and not get tossed out
              rotatingClockwise = true; // set the direction flag
              levelRotating = true; 
              StartCoroutine(SpinLevel(rotateSpeed, Vector3.forward, pivot));
              
          }
          else if (Input.GetAxisRaw("Rotate") > 0)
          {
              toAngle = Quaternion.AngleAxis(startDegree + rotationDegree, -levelTrans.forward); 
              this.transform.SetParent(levelTrans);
              rotatingCounterClock = true;
              levelRotating = true; 
              StartCoroutine(SpinLevel(rotateSpeed, -Vector3.forward, pivot));
              
          }
      } // else do nothing if the level is moving. No change in current location, no change in flags, no change in toAngle
  }
 
 

private IEnumerator SpinLevel(float overTime, Vector3 rotDirection, Transform pivot) {

      Vector3 pivotPoint = pivot.position;
 
      Vector3 destination = FindEndRotation(rotationDegree);
      Vector3 start = startAngle.eulerAngles;
 
     
 
      float dotRotateCheck = 0.0f;
 
 
      // if they are different, rotate the level a small amount, and set the level rotating to True
 
      while (!Mathf.Approximately(dotRotateCheck,0.0f))
      {
              
          levelRotating = true;
 
          levelTrans.RotateAround(pivotPoint, rotDirection, overTime * Time.deltaTime);
 
          dotRotateCheck = Vector3.Dot(levelTrans.transform.right, destination);
 
 
 
          yield return null; 
      }
 
 
          levelRotating = false;
 
          this.transform.parent = null;
          
      
  }
 
 
  private Vector3 FindEndRotation(float degree)
  {
      Vector3 end = new Vector3(0,0,0);
 
      float radians = degree * Mathf.Deg2Rad;
 
      end.x = Mathf.Cos(radians);
      end.y = Mathf.Sin(radians);
 
      
      return end;
  }


Thanks for any help you can provide

( ai also have tried using local forward z compared to rotation degrees, and vector3.angle to find the current angle in the loop until it reaches 0. I suspect that the issue is Float and Epsilon, but...

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 Lynkfox · Feb 25, 2019 at 03:25 AM

In the end, I feel silly as there was a much easier way to do it. The level design already calls for a centerpoint that the camera is focused on - and that is of course a game object.

I went back to my old rotate code, and moved the centerpoint to the master of the level instead of an empty game object. Rotating this of course rotates the entire level around a pivot, with ease.

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

167 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 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

Rotation Direction 2 Answers

Unity crashes when using while loop 0 Answers

Who can help me with RotateAround,it's unbelievable! 0 Answers

How To draw line or elastic line around circules 0 Answers

mathf.round problem 2 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