How to reverse a reading of degrees
Hey guys,
I'm working on a top down 2D space sim, and I've just run into a problem I didn't expect.
In my game, when the spaceship turns, the heading in degrees changes accordingly (north is 0, south is 180, etc). When I was doing this in 3D, it worked like a charm. All I had to do was calculate the heading using the following line of code:
heading = transform.eulerAngles.y;
But now that I'm working in 2D, with the Z axis pointing "downward", the heading DECREASES when the ship turns clockwise when what I need it to do is INCREASE, and vice versa when it turns counter clockwise.
Is there some type of formula or mathf function that can change the value to what I need it to be? I'm struggling to find a solution to this, and I'm hoping someone out there knows of one. Thanks in advance.
Test some angles and write a list of the number it says, and the angle it should say. That will help make a formula.
Reading off eulerAngles can give garbage, but reading only Y, if you never "roll", is safe. I think your formula is just 360-eulerAngles.y. But gather some values to check.
Like I was saying before, reading Y works fine, but when I read the Z value, it goes the opposite way that I need it to go. And I understand that it goes that way because of the direction that the Z axis points. What I'm trying to find is a way to somehow change that value so that it reads in reverse. Ins$$anonymous$$d of 359, I get 1, ins$$anonymous$$d of 270, I get 90, and so on. I suppose I could have a massive switch statement that changes all 360 numbers to their desired values, but I'm screwed if I ever need anything other than a whole number, plus I can't believe there isn't any more elegant solution, something mathematical, that would do the conversion.
Answer by MerlinsMaster · May 27, 2019 at 04:16 PM
I figured it out. For anyone who is having the same issue, here is the solution that worked for me:
heading = Mathf.Repeat(-transform.eulerAngles.z, 360f);
Credit goes to EmmaEwert in the Unity Forums for giving me this excellent solution.
Answer by Bunny83 · May 25, 2019 at 09:22 AM
The usual mathematical definition of an angle does increase in the counter clockwise direction, starting at 0 on the right (or "east" if you will). This is how all of trigonometry works.
I would generally recommend to not use eulerAngles. They suffer from gimbal lock and Unity uses quaternions internally to represent rotations. Though if you want you can use whatever you want.
If your angle has already the right offset (so 0 is where you want 0 to be), you can simply inverse the value:
heading = -transform.eulerAngles.z;
Depending on the range you want to use (0 to 360 or -180 to 180) you might need to perform a proper roll over. For the range 0 to 360 you have to do
if (heading < 0)
heading += 360;
if (heading > 360)
heading -= 360;
for the range -180 to 360 you have to do
if (heading < -180)
heading += 360;
if (heading > 180)
heading -= 360;
Instead of eulerAngles you might want to use Vector3.SignedAngle with a direction vector of your object. Note that SignedAngle always return a value between -180 and 180. If you want 0 to 360 just use
if (heading < 0)
heading += 360;
"you can simply inverse the value: heading = -transform.eulerAngles.z;"
That's the first thing I tried, and it just returned the same value, only negative (356 was -356, etc).
"Depending on the range you want to use (0 to 360 or -180 to 180) you might need to perform a proper roll over."
I tried that, but it just returned the same value I had already.
"Ins$$anonymous$$d of eulerAngles you might want to use Vector3.SignedAngle with a direction vector of your object. "
Then I tried this, first with Vector3.SignedAngle, but it wasn't returning anything but zero, so I thought it must be because I'm working in 2D, so I tried the following:
Vector2 shipDir = shipTransform.position - transform.position;
Vector2 forward = transform.forward;
float angle = Vector2.SignedAngle(shipDir, forward);
heading = angle;
But the result was the same. It just returned zero, no matter which direction my ship was pointed in.
Your answer
Follow this Question
Related Questions
How do I make my character move on the Z axis on 2d rigidbody? 0 Answers
2.5 D, Unity 5, Animations wont work while moving on the Z-Axis. 1 Answer
Game Object jumps to z-axis 0 Answers
Detect whether a Rigidbody collided with a Trigger 2 Answers
How do I maintain the same X and Y coordinates when changing scenes? 1 Answer