Reading rotation values, applying them to GameObjects in Unity and reading them back gives different results

58 Views Asked by At

I am creating a road editor in Unity. I have the data of each road segment that comes after another stored in a text file. It also includes 0-360° values for the segments rotation.

For example, the road file describes a spiral by incrementing the tilt [x] value by 5 every other segment. I apply the values like so:

segment.transform.rotation = Quaternion.Euler(segment.tilt, segment.turn * -1f, segment.height);

As you have noticed, I invert the turn value as otherwise the track is mirrored for some reason.

The track and the spiral in it looks perfectly fine in Unity. Now I want to write the data back to the text file. The track is unchanged, so in the best scenario I expect exactly the same values.

When using this line of code to obtain the values: segment.transform.rotation.x I get only 0 as value. I then tried segment.transform.localEulerAngles.x. Here it looks good until the tilt value reaches 90°. Then, instead of the next value being 95°, it goes back to 85°.

So for example:

90.00000     0.00000    180.00000     // This line is OK, the spiral is at 90 degrees
85.00000   180.0000       0.00000     // This next line is not okay!

95.00000     0.00000    180.00000     // This is what I need instead

Another random example [not inside the spiral]:

0.00000     171.37701    -4.24026     // What I read from the file the first time
0.00000     189.00000   356.00000     // What I get back from Unity   

I guess I need to do some fancy mathematical calculation to get the correct values but I just can't wrap my head around it. Can anyone shed some light?

0

There are 0 best solutions below