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 GameSlayerGS · Apr 22, 2013 at 03:01 PM · rotationgameobjectrotate

How do I rotate an object a certain number of degrees?

My goal is to rotate a GameObject a certain number of degrees. I currently have a script that rotates it indefinitely, or it should, but it slows down and stops at 180, though I did nothing to specify that. I assume there's something built into Unity that does that, which isn't a big deal, I can adjust the rotation in the Inspector manually beyond 180, but the script stops at 180, how can I have it rotate until it reaches a certain number of degrees even if that is past 180? (Also, the "rotation1" variable never goes past 1, I don't think I should need that, but it's odd that it doesn't go past 1.)

private var rotation1:float = 0;

function Reset () { transform.rotation.z = 0; }

function Update () {

 transform.rotation.z = (transform.rotation.z + 1 * Time.deltaTime);    
 rotation1 = (transform.rotation.z);
 print(rotation1);
 if (rotation1 == 180) {
     Reset();
 }

}

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
2
Best Answer

Answer by robertbu · Apr 22, 2013 at 03:18 PM

Transform.rotation is a Quaternion...a 4D construct that you should not manipulate directly unless you fully understand quaternions. You can manipulate Transform.eulerAngles, but you have to be careful about how you do the manipulation. From the reference:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.

In addition, there are multiple euler angles that represent the same 'physical' rotation. So for example you could do something like...

transform.eulerAngles = Vector3(180,0,0);

...and if you immediately read back the value you might bet a reading of (0,180,180), which represents the same physical rotation.

One way to deal with Transform.eulerAngles is to manage it as "write-only." That maintain your own Vector3 value and set it when needed...and it will not stop at 180.

Comment
Add comment · Show 1 · 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 GameSlayerGS · Apr 22, 2013 at 03:45 PM 0
Share

Thanks! I'm somewhat new to Unity, and prior to Unity all my scripting was done in Lua, so there's a lot of things that I end up doing wrong.

avatar image
4

Answer by proandrius · Apr 22, 2013 at 03:47 PM

You should use transform.Rotate(new Vector3(0,0,1));

and if(Mathf.Round(transform.eulerAngles.z)==180) { transform.Rotate(new Vector3(0,0,-180)); }

Comment
Add comment · Show 1 · 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 lingoded · Aug 22, 2015 at 12:32 PM 0
Share

Thank you so much! I was trying to get my head around this. Really, It's good to know the method library for unity to simplify things alot.

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

13 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

Related Questions

How get the rotation of an GameObject? 3 Answers

Rotate around a locally tilted axis?, 1 Answer

Rotation is jumpy 2 Answers

Cannot rotate GameObject from C# Animator attached 1 Answer

How to slowly rotate a gameObject from 0 to -90f and vice versa every 5 seconds? 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