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 dkh · Feb 14, 2013 at 01:26 AM · rotationtransformquaternioneulerdifference

Pitch/yaw difference between 2 transforms

Question: I have two transforms in my scene. The first one is my camera, the second one is a spaceship. Both of these can move and rotate freely (6dof) in the scene. I need to get the pitch/yaw difference between both transform.rotations, ie. the angles at which I'm looking at the ship.

Examples: If the spaceship is facing straight away from us and we're right behind the ship's tail, the yaw should be 180 deg, pitch 0 deg for example. Or if the ship is facing left (relative to the camera) and we're seeing it from the top-down (ie. we see its 'roof' only), the yaw and pitch would be 90 deg both.

Attempt: I tried the following after reading this:

 int CalculateYaw()
 {
     // get a vector from the ship to the camera
     Vector3 shipToCamera = Camera.main.transform.position - transform.position;
     
     // get a numeric angle for each vector, on the X-Z plane (relative to world forward)
     float angleA = Mathf.Atan2(transform.forward.x, transform.forward.z) * Mathf.Rad2Deg;
     float angleB = Mathf.Atan2(shipToCamera.x, shipToCamera.z) * Mathf.Rad2Deg;
     
     // return the signed difference in these angles
     return (int)Mathf.DeltaAngle(angleA, angleB);
 }
 
 // same for pitch along Y-Z plane

But the code doesn't work to my expectations. When the camera is at 0/0/0 and the spaceship at 0/0/30 in the scene and I let the spaceship rotate along its up-axis, the yaw behaves as it should (ever incrementing) but the pitch jumps between 0 (where it should be at) and 180 - being at 180 whenever the ship is facing away from the camera and 0 when it's facing toward me. I suspect this is due to the angles being relative to the world forward when they should be relative to the spaceship transform forward.

And this is where I'm lost, any help/insight would be greatly appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by earok3d · Nov 18, 2014 at 12:12 AM

Apologies for this being well over a year late, is this what you required?

 public static float DeltaYaw(Transform source, Vector3 destination)
 {
     var destination2 = source.InverseTransformPoint(destination);
     return (Mathf.Atan2(destination2.z,destination2.x) * Mathf.Rad2Deg) - 90;
 }
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

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

10 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

Related Questions

How can I multiply the rotation of a mirrored object? 1 Answer

Problem with Quaternion.Euler() when rotating object 3 Answers

How to Play a Sound Every Time a X,Y,Z Coordinates Change? 3 Answers

How do you manually override mecanim's interpolation with rotations? 2 Answers

How do I read the Transform's rotation value from the inspector's panel of a gameobject or transform? 2 Answers


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