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 drudiverse · Apr 06, 2014 at 05:22 AM · rotationangle

find angle between gameobject and world axis?

this didn't work and i dont know why:"

 Vector3.Angle(go.transform.eulerAngles ,transform.forward);

the result stays stuck at 90 for any go.rotation

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

2 Replies

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

Answer by Owen-Reynolds · Apr 06, 2014 at 06:19 AM

You wrote world axis. So don't you think the equation should have that? For example, X is Vector3.right (or new Vector3(50,0,0), etc... anything with positive X and 0 for Y and Z.)

transform.forward is the direction the object is facing (the blue arrow, which is where it "counts as" facing. If it isn't, most people fix it.)

So, to get the angle between object facing and right facing, use Vector3.Angle(transform.forward, Vector3.right). The angle is if you put the starting ends of both arrows together. 0 means you're facing right. 10 means you facing almost right -- titled 10 degrees up or down from that. If you need to know which one (often you don't) you can search around here for that trick. It's also 3D, so 10 could mean you're looking mostly right, but 10 degrees up.

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
2

Answer by supernat · Apr 06, 2014 at 06:18 AM

eulerAngles is not a directional vector, so this won't work. Vector3.Angle is used to find an angle between two directional vectors, i.e. vectors that when normalized would point in the direction something is facing. The vectors don't necessarily have to be connected in any way, but usually it makes more sense to consider at least one of the ends points to reside at the same position. There are cases where you might just want the angular difference between two headings of two different objects.

I'm not sure exactly what you mean by finding the angle between the game object and the world axis. If you mean finding the game object's angle relative to the world axis, that's just eulerAngles. If you wanted the game object's angles relative to its parent, it would be localEulerAngles.

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 drudiverse · Apr 06, 2014 at 09:02 AM 0
Share

it s for a spacecraft turning towards the cursor position and flying towards Z axis, i wanted to clamp the rotation so that it never goes more than 45 degrees away from z axis.

so i just wanted the vector of the spacecraft and of the world z axis, and for some reason it's beyond me!

the solution is var deviation = Vector3.Angle(transform.forward, Vector3.forward);

Thanks!!!

avatar image supernat · Apr 07, 2014 at 04:21 PM 0
Share

I gotcha, yes I misunderstood, I probably would have worded the question in the same manner, so no worries. I'm sure you don't really care now that it's working with Angle(), but what that method is doing is computing the dot product between two vectors, then dividing that dot product by the magnitude of both vectors, then taking the acos() of the result to get your angle.

The magnitude part is a problem if you are looking for optimized code, because it uses the expensive sqrt operation twice. In most cases this won't be an issue (on mobile or consoles maybe a problem), but if you're trying to squeeze every bit of efficiency out, you can compute the dot product yourself, then just take the acos() of the dot product, because in your case Vector3.forward and transform.forward both have magnitudes of 1.0 (they're unit vectors). In fact, Vector3.forward never changes, and two components are 0, so you can actually get the angle by just saying acos(Vector3.forward.z*transform.forward.z) assu$$anonymous$$g the Vector3.forward value is 0,0,1 (and even this can be further reduced to just transform.forward.z). Just for fun. :)

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

23 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

Related Questions

How to use the lerp function with rotation? (C#) 1 Answer

How do I add force to an object based on my Camera's cureent Y rotation (C#) 2 Answers

rotation x changes all angles? 3 Answers

local rotation per axis between frames? 1 Answer

Angle of parametric path 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