Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by alexanderameye · Jan 23, 2015 at 07:37 PM · rotationconvertanglesy-axisdegrees

Getting cosinus of an angle in degrees

Hey, so I have an object in a scene rotated around the y-axis with an angle of 45 degrees. When I put this line of code in the script and run it:

         print (Mathf.Cos (transform.rotation.y));

The editor returns the value '0.927...'. Is this the value in radians? because I checked and the cos of 45 degrees is 0.707. and if so, how can I get Unity to use 0.707 instead of 0.927?

and I also I thought the cos of 45 deg in rad was pi/4 rad and that is 0.78

Thank you very much!!!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · Jan 23, 2015 at 07:44 PM

http://docs.unity3d.com/ScriptReference/Mathf.Rad2Deg.html

http://docs.unity3d.com/ScriptReference/Mathf.Deg2Rad.html

Degrees = (Radians x 180) / Pi

Radians = (Degrees x Pi) / 180

Comment
Add comment · Show 4 · 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 alexanderameye · Jan 23, 2015 at 07:45 PM 0
Share

but still the cos of 45 degrees in rad isn't 0.9 right?

avatar image meat5000 ♦ · Jan 23, 2015 at 07:52 PM 0
Share

You can work this out by putting in the extreme values.

Try cos(90), cos(180) etc and see what the results are. The extremes should give answers of 0,1,-1 if you look at a graph.

(Cos(45) in degrees gives an answer of 0.707)

avatar image alexanderameye · Jan 23, 2015 at 08:07 PM 0
Share

Tyvm, this helped me a lot!

avatar image meat5000 ♦ · Jan 24, 2015 at 10:22 PM 0
Share

You're Welcome.

avatar image
1

Answer by tanoshimi · Jan 23, 2015 at 07:53 PM

 transform.rotation.y

is not what you think it is. It's the y component of the rotation expressed as a quaternion (which is basically black magic). What you're looking for is the rotation around the y axis as an angle expressed in degrees, which is:

 transform.eulerAngles.y
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 alexanderameye · Jan 23, 2015 at 08:06 PM 0
Share

Ok, this solved the issue of the value being weird, now they are what I would expect them to be in radians, I'll just need to convert them to degrees now, tyvm!!

avatar image
1

Answer by Owen-Reynolds · Jan 23, 2015 at 08:02 PM

You almost never have to use cos/sin in Unity. If you back up to what you're trying to accomplish, there's probably a nice Unity built-in that will do it, w/o needing any math.

Misc problems: 1) rotation.y is a (useless) Quaternion value. It isn't degrees or radians. transform.eulerAngles.y will give the value you see in the Inspector for rotation. 2) transform angles are in degrees. Like all computer cos/sin functions, Unity Cos expects the input to be in Radians. 3) Radians have 0 facing right and go CCW, while Unity degrees have 0 forward and go CW. This sort of thing is common, and you'll probably find better info in (non-unity) trig forums.

But, really, if you are even asking if the value of cosin is in radians (it isn't in degrees or radians!) then avoid trig and use builtins. For example, transform.right gives the length 1 right-facing arrow (the red X arrow in the Scene window.) So transform.right.x will give a sort of cosine, if the object's Y-axis a straight up. But, you may not even need cosine to do what you really want.

Comment
Add comment · Show 2 · 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 alexanderameye · Jan 23, 2015 at 08:10 PM 0
Share

I have a door object, and I need to place an object on runtime on the side of the door to function as a hinge on EVERY possible door. so position.x of the hinge = position.x of the door - scale.x of the door/2

I think I need cos to figure out where to place the hinge when the door is rotated

avatar image Owen-Reynolds · Jan 23, 2015 at 09:58 PM 0
Share

Easier to use transform.right*(scale.x/2), or to give each door-type an empty "mountPoint." There are a bunch of old answered Q's about positioning objects based on other objects.

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

20 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

Related Questions

Trying to get the angle of a camera object based on players forward, is not distance independent 0 Answers

Clamp rotation of sprite? 0 Answers

Is there a way to convert a euler vector from 0 to 360 --> -90 to 90? 0 Answers

Quick Angles Question 2 Answers

How to check object "rotated" X degrees 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