- Home /
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
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/
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
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;
}
}
}
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.
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;
}
}
}
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 ----
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