Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MerlinsMaster · May 25, 2019 at 07:07 AM · 2d gamez-axis

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.

Comment
Add comment · Show 2
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 Owen-Reynolds · May 25, 2019 at 05:15 PM 0
Share

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.

avatar image MerlinsMaster Owen-Reynolds · May 27, 2019 at 02:17 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

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.

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 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;



Comment
Add comment · Show 1 · 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 MerlinsMaster · May 25, 2019 at 04:38 PM 0
Share

"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

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

133 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

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


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