0

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?

doc4
  • 3
  • 3
  • right, but -4 is also 356 in terms of degrees of a circle. its like going 2 ways round the sqaure, both end up at the diagnoal corner but one went left one went up – BugFinder Jun 25 '23 at 14:22
  • The values you show are expressing the exact same orientation, except in different ways. – Bart Jun 25 '23 at 16:03
  • Yes, that is correct. Unity can handle the values in both cases but the game where I will place the edited file can't. It needs the values not being negative, and tilt above 90. If I import the track into the game with the Unity modified file, the spiral goes completely haywire after the first 90 degrees. – doc4 Jun 25 '23 at 17:11
  • You could store the Quaternion representation instead, then there is no ambiguity in the rotation it represents. The problem is that Euler angles can represent the same rotation multiple ways. Unity converts the Quaternion to an Euler angle equivalent anyway when you use eulerAngles. – hijinxbassist Jun 26 '23 at 19:56
  • Thank you for your input. Unfortunately, the Quaternions gave me the same values as shown above. However, I created my own method now to re-calculate the values. I am sure this is a rather hacky approach and there are better solutions, but it gets the job done and maybe you can see what I want to achieve. The method call: `Vector3 angles = CalculateAngles(lastSegment.transform.eulerAngles, segment.transform.eulerAngles);` [And here it is](https://pastebin.com/bvUDqx7q) I unfortunately need to do the same now with loopings [z value] but at least I got a solution that works... – doc4 Jun 27 '23 at 13:05

0 Answers0