Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 HarD-izzeR · Jun 05, 2018 at 07:55 PM · rotationeditorinspectoraxiseuler angles

How to get object's X axis rotation EXACTLY as in inspector ?

Hello, I just want to know How to get object's X axis rotation EXACTLY as in inspector ? I tried transform.eulerAngles.x it works at the beginning but when the X axis in the inspector hits 83 the one in the script starts to decrease on its own, and when the one in the script hits 0 it goes up to 340 directly.. and I also tried transform.rotation.x and when the x axis in inspector hits like 10 the one in the script is something like 0.9465872 O_o

So how to get the EXACT same value in the inspector's Transform window ?

Thanks

Comment
Add comment · Show 1
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 MT369MT · Jun 05, 2018 at 09:28 PM 0
Share

HI, if you call transform.rotation.x it will give you the cos(angle). I found this answer here:

https://forum.unity.com/threads/solved-how-to-get-rotation-value-that-is-in-the-inspector.460310/

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Jun 06, 2018 at 12:44 AM

The short answer is: You can't get the exact value as the editor will actually cache the value internally for editing. The problem is that Unity itself (the engine) does not work with euler angles at runtime but with quaterions. When you read transform.eulerAngles it will actually convert the quaterion to eulerAngles. Since eulerAngles are not a unique representation of an orientation there are several ways how the same orientation could be represented. Just as an example the eulerangles (0,0,0) is the same as (180,180,180) or (180, -180, 180).


The editor tracks a seperate angle value for editing in the inspector. It's used when you actually edit the rotation in the editor. This value does not wrap around but simply continues to increase / decrease.


If you want the eulerangles to be in a certain range, you need to adjust them yourself. For example if "y" should never be negative, just add 360 if the value that transform.eulerAngles.y returns is negative. X usually has the range of +- 90°. Anything larger could be represented by an angle smaller than 90 and y and z rotated by 180°. So the angle (100,30,0) would be the same as (80,210,180).


As i said there simply is no "correct" or unique way how the eulerangles should represent a certain rotation. Since you haven't mentioned your actual problem you want to solve we can't suggest a different approach. You shouldn't rely on the eulerangles conversion in the first place.


If you have never heard about Quaternions and you want to know how they work, have a look at this Numberphile video

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
avatar image
0

Answer by HarD-izzeR · Jun 06, 2018 at 10:15 AM

Ok, Here is the problem: I made a day/night cycle script (just directional light rotating in X axis in a variable speed) I also have some low poly game objects (clouds) And I want the opacity of their materials (Aplha channel) to be equal to the sun movement So that if its day time the clouds would be visible and its fading out, and when its night time they are invisible and fading in.. All of this are working in my script but the timing of fade in/fade out are wrong... I tried to debug.log the rotation of x axis (which I didnt know its impossible..) and it came wrong... Here is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DayNight : MonoBehaviour {
     public GameObject[] clouds;
     public float speed = 1.0f;
     public float sunPos;
 
     void Update() {
         transform.Rotate(speed * Time.deltaTime, 0, 0);
         sunPos = transform.eulerAngles.x;                // here is the problem
         clouds = GameObject.FindGameObjectsWithTag("Clouds");
         foreach (GameObject item in clouds) {
                 Color color = item.GetComponent<Renderer>().material.color;
                 color.a = 1 - sunPos;
                 item.GetComponent<Renderer>().material.color = color;
         }
     }
 }

 
Comment
Add comment · Show 10 · 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 Bunny83 · Jun 07, 2018 at 01:23 AM 0
Share

And here we have the problem. You try to use a value for something it's not meant for. You should first create a seperate time variable which defines the current time of your world and then just create an absolute rotation that matches the time. Again, eulerAngles are not a value stored in the transform but it's calculated from the object's rotation. You already have your "sunPos" variable. Rename it to "time" and reverse your logic:

 void Update()
 {
     time += speed * Time.deltaTime;
     if (time > 1f)
     {
         time = 0f;
         day++;
     }
     transform.rotation = Quaternion.Euler(time * 360,0,0);
 }

This assumes "time" goes from 0 to 1. So if 0 is midnight, 0.5 will be noon. So if you multiply this "time" by 24 you actually get the current hour of the day. This will also allow you to set the current time more easily. So ins$$anonymous$$d of relying on the current rotation of an object you actually dictate the rotation based on the time.

avatar image HarD-izzeR · Jun 07, 2018 at 04:04 PM 0
Share

Ok Thanks @Bunny83 I dont really understand... What is this day++ ? Will it be something like this : ?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DayNight : $$anonymous$$onoBehaviour {
     public GameObject[] clouds;
     public float speed = 1.0f;
     public float time;
 
     void Update() {
 
         time += speed * Time.deltaTime;
         if (time > 1f) {
             time = 0f;
             day++;
         }
         transform.rotation = Quaternion.Euler(time * 360, 0, 0);
 
         clouds = GameObject.FindGameObjectsWithTag("Clouds");
         foreach (GameObject item in clouds) {
             Color color = item.GetComponent<Renderer>().material.color;
             color.a = 1 - time;
             item.GetComponent<Renderer>().material.color = color;
         }
     }
 }
avatar image Bunny83 HarD-izzeR · Jun 07, 2018 at 04:19 PM 0
Share

I just added "day++" to make it more clear that when time wraps around 1 day has passed. $$anonymous$$eep in $$anonymous$$d that the time value of 0 and a value of 1 actually means the same: midnight. Since you want to have the clouds invisible at night but visible during the day you probably want something like:

 color.a = $$anonymous$$athf.PingPong(time * 2, 1f);

This will yield a value of 0 at a time of 0 as well as 1. However it will return a value of 1 at a time of 0.5 (noon). Have a look at this table:

   time | time*2 | $$anonymous$$athf.PingPong
 #------------------------------
   0    |  0     | 0     ----
   0.25 |  0.5   | 0.5     fading in
   0.5  |  1.0   | 1     ----
   0.75 |  1.5   | 0.5     fading out
   1.0  |  2.0   | 0     ----

avatar image HarD-izzeR Bunny83 · Jun 07, 2018 at 05:38 PM 0
Share

Ok Thanks alot it works.. But just the ti$$anonymous$$g is wrong... It should be the opposite.. When I play (I start at day (sun rot x is 0)) the clouds are invisible and fading in and when its night the clouds are visible and fading out. It should be the opposite

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

138 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

Related Questions

Is there a way to access the inspector fields (not the component properties) through a unity editor script? 0 Answers

Unity Rotation Bug (Video) 2 Answers

How to specify the rotation angle in degrees, in the inspector? 2 Answers

Duplicating rotation with axis constraints 1 Answer

Dealing with rotating objects on a dynamic axis 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