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 Normantas · Aug 07, 2018 at 08:49 PM · rotationerrorquaternion

My Code that Works on an object's rotation doesn't work.

I'm coding a system, where if my 3d object moves forward, it's rotation goes down to a certain point, when I try my code, it will keep rotating for ever when I use it (pressing W).... Code:


 if (Input.GetKey(KeyCode.W))
 {
     transform.Translate(0f, 0f, speed * Time.deltaTime); // Forward movement of the player's character.

     if (transform.rotation.x <= 20) //If the rotation of the object is below or = 20, make the rotation higher
     {
         transform.Rotate(rotationSpeedWhileMoving * Time.deltaTime, 0f, 0f);
     }
 }
 else if (transform.rotation.x > 0) //Returns THe object's rotation to 0
 {
     transform.Rotate(-1 * rotationBackwardsSpeed * Time.deltaTime, 0f, 0f);
 }
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

Answer by Hellium · Aug 07, 2018 at 09:17 PM

transform.rotation is a QUATERNION, you should not try to read and write values of it, unless you really know what you are doing, and it clearly seems that it's not the case.

You should check against transform.localEulerAngles instead, which are the values displayed in the inspector:

 if (transform.localEulerAngles.y <= 20)
 // ...
 if (transform.localEulerAngles.x > 0)
Comment
Add comment · Show 5 · 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 Normantas · Aug 08, 2018 at 10:01 AM 0
Share

I should exchange eulerAngles with localEulerAngles?

avatar image Harinezumi Normantas · Aug 08, 2018 at 10:38 AM 0
Share

No, change transform.rotation.x to either transform.rotation.eulerAngles.x or transform.localEulerAngles.x.

avatar image Hellium Normantas · Aug 08, 2018 at 10:59 AM 0
Share

transform.eulerAngles is the global rotation of the object.

transform.localEulerAngles is the local rotation of the object, the values shown in the inspector.

avatar image Normantas Hellium · Aug 08, 2018 at 03:26 PM 0
Share

What is the difference between local and global rotation?

Show more comments
avatar image
0

Answer by tormentoarmagedoom · Aug 08, 2018 at 07:35 AM

Good day.

As @Hellium says, in Unity, standard variable for rotation are QUATERNION (there are not the "typical" 3 rotation of each axis). In the inspector, you see the EULER ROTATION (not quaternion), so if you want to manipulate rotation, you should do it via Euler angles (they are indeed the 3 axis rotation from 0 to 360).

You should check against

 transform.localEulerAngles

instead:

Go try it, and if don't suceed, come again!

Bye!

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 Normantas · Aug 08, 2018 at 05:52 PM 0
Share

This is where I'm stuck now

if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) { transform.Translate(0f, 0f, speed * Time.deltaTime); // Forward movement of the player's character.

         if (transform.localEulerAngles.x <= 20) // Transforms the rotation of the object a bit down.
             transform.Rotate(rotationSpeedWhile$$anonymous$$oving * Time.deltaTime, 0, 0);
     }
     else if (transform.localEulerAngles.x > 0 && transform.localEulerAngles.x < 40) //returns the rotation back to normal.
         transform.Rotate(-rotationBackwardsSpeed * Time.deltaTime, 0, 0);

the 'else if' part works fine, but the if inside the if statement doesn't work at all.

avatar image Hellium Normantas · Aug 08, 2018 at 06:37 PM 0
Share

The problem I see with your code is that when the rotation reaches 20 degrees, the if is evaluated to false, and the else block is ran, thus, the object rotates back to a value less than 20 and the if is ran the next frame, the object rotates a little bit and the process repeats : else is ran, if is ran next frame, else, if..... again and again....

avatar image Normantas Hellium · Aug 08, 2018 at 07:24 PM 0
Share

So how do I fix this?

Show more comments

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

149 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

Related Questions

Quaternion to Matrix Conversion Error - Camera Script 0 Answers

rotate problem pls help 1 Answer

Position errors on rotated child objects? 0 Answers

Quaternion To Matrix conversion failed because input Quaternion is invalid 1 Answer

Gun Rotation 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