Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This question was closed Jul 26, 2015 at 03:56 AM by AlwaysSunny for the following reason:

Duplicate Question

avatar image
0
Question by Bengooch7 · Jul 26, 2015 at 03:29 AM · rotationjavascriptscripting problemeuleranglesdaynight

transform.rotation.z gives different value than expected

I'm got a game object that has it's z-axis rotated to a new position when the scene starts, based on the time of day. I want the color of the game object to change depending on how many degrees the object's z-axis rotation is at. The if statements are failing because the value I'm getting returned is less than 1.0, but the rotation in the editor says it's like 221 degrees.

What am I missing here? note this isn't the whole script, I didn't copy over anything not related to this issue.

The IF statements in the getSunState() always execute the first one because the results of this.transform.rotation.z always com in below "1".

 //Lerp colors of sun to simulate sunrise
 function sunRise(){
     Debug.Log("sunRise called 3");
     sun.color = Color.Lerp (sunNightColor, sunDayColor, Time.deltaTime * 21600);
 }
 
 //Lerp colors of sun to simulate sunset
 function sunSet(){
     Debug.Log("sunSet called 3");
     sun.color = Color.Lerp (sunDayColor, sunNightColor, Time.deltaTime * 28800);
 }
 
 function getSunState() {
 Debug.Log("getSetState called 2");
 Debug.Log("transform.rotation.z = " + transform.rotation.z);
     while (normalPassageOfTime == true) {
         if (transform.rotation.z > 75 && transform.rotation.z < 120) {
             sunRise();
         }
         if (this.transform.rotation.z >= 120 && this.transform.rotation.z < 240) {
             sun.color = sunDayColor;
             Debug.Log("Sun color set to Day 3");
         }
         if (this.transform.rotation.z >= 240 && this.transform.rotation.z < 285) {
             sunSet();
         }
         if (this.transform.rotation.z >= 165 || this.transform.rotation.z <= 75) {
             sun.color = sunNightColor;
         }
         yield;
     }
 }
 
 //set position of the sun based on time of day
 function setSun() {
     Debug.Log("setSun function works 1");
     transform.Rotate(0,0,degreesOfRotation * (hoursInSeconds + minutesInSeconds));
     getSunState();
     Debug.Log("z rotation = " + this.transform.rotation.z);
     Debug.Log("sunRise called 3");
 }
 
 
 
 //rotate the sun based on set speed
 function moveSun() {
     while (speedOfLight > 0) {
         //Multiplies speed variable by number of degrees to rotate per second
         speedModifier = degreesOfRotation * speedOfLight;
         transform.Rotate(0,0,speedModifier * Time.deltaTime);
         yield;    
     }
 }


 function Start () {
     
     normalPassageOfTimeToggle();
     importVariables();
     if (normalPassageOfTime == true) {
         setClockHour();
         transform.rotation = Quaternion.identity;
         setSun();
         moveSun();
     }
     
     //Debug.Log("TODPhrase within Start function is " + timeOfDayPhrase);
     //Debug.Log("clockHour within Start function is " + clockHour);
 }
 
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

  • Sort: 
avatar image
0
Best Answer

Answer by AlwaysSunny · Jul 26, 2015 at 03:51 AM

As you can see in the transform.rotation documentation, that value is of the Quaternion type.

Reading and writing directly to the component values of a quaternion is not really a thing. They don't exactly have meaning apart from their mathematical relationship to their sibling values, and are not for humans to manipulate. Quaternions are for computers to deal with.

You can use basic trigonometry and the Vector3 and Quaternion class methods to achieve all the rotation-related goals you'll have in Unity. When you need to work directly with specific angle or radian values, you can use those formula and methods which take or return those units. Which formula or method to use varies case by case, and is something you'll pick up as you encounter and research specific needs. Eventually it'll click - how certain approaches work better in certain cases - after which you'll rarely struggle with rotation-related goals.

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 Ness · Jul 26, 2015 at 03:54 AM 1
Share

or just use transform.eulerAngles ins$$anonymous$$d transform.rotation ;)

avatar image Bengooch7 · Jul 26, 2015 at 01:13 PM 0
Share

Thanks for the link and the explanation, @AlwaysSunny. I would have accepted you answer as "best", but I see you've taken the liberty of doing that already.

@Ness, I switched to eulerAngles and it's working perfectly now, thanks!

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

Saving Runtime Changes (JS) 1 Answer

Why does my rotation lock? 2 Answers

Limit Horizontal Rotation 1 Answer

Smooth rotation in 90° increments 0 Answers

Rotation drift with transform.eulerAngles 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