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 bartleycollin · Apr 03, 2014 at 01:50 AM · rotationissueitweenglitchuneven

iTween - Rotation Add get's uneven after several rotations

I hope it's alright if I post an iTween question here... Either way, here it is.

My current setup is a camera on my screen, that is set to rotate 90 degrees when you hit the right or left arrows. (it rotates 90 degrees left, with the left arrow, 90 degrees right with the right arrow.)

Here lies my problem. If I rotate it to the left say, it will be fine. Same for the right. But if I keep rotating it different directions, eventually, instead of being a solid number (remember, all I'm doing is adding 90 degrees left and right) it's something like 180.4532 degrees, or 90.346. If I keep doing this, it get's worse. All I'm doing is using RotationAdd to add 90 to the Z of my camera when I press a key. Why would it be doing this?

-Thanks

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

Answer by robertbu · Apr 03, 2014 at 04:53 AM

Anytime you have a relative rotations, there will be drift. Part of the drift is related to the the imprecision of floating point numbers. In addition, it is easy when coding relative rotations to under or overshoot a bit on the last frame. Regardless of why it is happening, the solution is to use an absolute rotation. If you are only rotating by 90 on one axis, then keep a integer count of the number of 90 degree turns. Add or subtract one from the integer count and then use the result in an absolute rotation. For example iTween.RotateTo(). Or you could keep your own float value and round it to 90 each time you increment/decrement it.

Comment
Add comment · Show 4 · 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 bartleycollin · Apr 03, 2014 at 01:03 PM 0
Share

I can picture this in my head, but I'm having a hard time understanding how I would code this.. An example would be fantastic.

avatar image robertbu · Apr 03, 2014 at 06:46 PM 0
Share

Here is a bit of example code. One of the problems with using iTween is that you have to deal with the potential of multiple iTweens fighting. I handle this in the example code by use an 'oncomplete' hash value and forcing the user to wait until the current rotation is complete before allowing a new one to start:

 #pragma strict
 
 var rotCount = 0;
 var rotating = false;
 
 function Update() {
     if (!rotating && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
         rotCount = (rotCount + 1) % 4;
         DoRotation();
     }
     
     if (!rotating && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
         rotCount = (rotCount - 1) % 4;
         DoRotation();
     }
 }
 
 function DoRotation() {
     rotating = true;
     var rot = rotCount * 90.0;
     iTween.RotateTo(gameObject, {"z":rot, "oncomplete":"EndRotation", "time":1.25});
 }
 
 function EndRotation() {
     rotating = false;
 }
avatar image bartleycollin · Apr 03, 2014 at 08:03 PM 0
Share

Although I'm using C# I completely understand what you are saying. Let me see what I can do.

avatar image robertbu · Apr 03, 2014 at 08:09 PM 0
Share

Here is a C# translation:

 using UnityEngine;
 using System.Collections;
 
 public class Example : $$anonymous$$onoBehaviour {
 
     int rotCount = 0;
     bool rotating = false;
     
     void Update() {
         if (!rotating && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
             rotCount = (rotCount + 1) % 4;
             DoRotation();
         }
         
         if (!rotating && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
             rotCount = (rotCount - 1) % 4;
             DoRotation();
         }
     }
     
     void DoRotation() {
         rotating = true;
         var rot = rotCount * 90.0;
         iTween.RotateTo(gameObject, iTween.Hash("z",rot, "oncomplete","EndRotation", "time",1.25));
     }
     
     void EndRotation() {
         rotating = false;
     }
 }
avatar image
0

Answer by JoeysLucky22 · Dec 14, 2014 at 08:51 AM

Thanks for the snippet above! I wanted to rotate the object regardless if it was currently rotating or not so I just did this:

  public class Example : MonoBehaviour {
  
      private int rotCount = 0;
      private bool rotating = false;
      
      void Update() {
          if (Input.GetKeyDown(KeyCode.LeftArrow)) {
              rotCount = (rotCount + 1) % 4;
              DoRotation();
          }
          
          if (Input.GetKeyDown(KeyCode.RightArrow)) {
              rotCount = (rotCount - 1) % 4;
              DoRotation();
          }
      }
      
      void DoRotation() {
          float rot = rotCount * 90.0f;
          if(rotating){
              //If the object is currently rotating, instantly rotate it to its destination
              iTween.RotateTo(gameObject, iTween.Hash("z",rot, "oncomplete","EndRotation", "time",0));
          }
          rotating = true;
          iTween.RotateTo(gameObject, iTween.Hash("z",rot, "oncomplete","EndRotation", "time",1.25));
      }
      
      void EndRotation() {
          rotating = 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

22 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

Related Questions

Autorotation issue when exiting gamecenter 0 Answers

iTween Path 2D rotation, orientToPath issue. 1 Answer

Can't lock rotation of object. 0 Answers

LookAt inaccurate rotation issue (javascript) 1 Answer

iTween Rotation not exact 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