• 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 Michel Verheughe · Mar 21, 2013 at 09:13 PM · angle

Geographical bearing of an object

A ship sails at night in a harbour, following a leading sector light. On a bearing between 200 and 190 degrees, the light is seen red, between 190 and 170, white and between 170 and 160, green.

I need to know when making the light red, white or green. As I used to work with Flash ActionScript, I would calculate the arc tangent of the difference between the X coordinates and the Y (or rather, Z for Unity3D). But, somehow I feel that I just need to call a Unity3D function but I don't know which one. I tried:

 var angle = Vector3.Angle(lighthouse.transform.position, ship.transform.position);

and I get something but not the actual angles from 160 to 200. I have looked at many answers on this forum but they all seem to address the relative difference of heading between two objects. I only need to know the bearing from the object as a global value since z positive is the north, for me.

Thanks in advance,

Cheers, Michel

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
0
Best Answer

Answer by robertbu · Mar 21, 2013 at 10:03 PM

First, Vector3.Angle produces then unsigned angle between two vectors using all x,y, and z of the two vectors. Second, you are calculating that position, not relative to the ship, but relative to the origin. You have a few choices. You can use Mathf.Atan2() to calculate an absolute angle on the plane relative to the ship. Assuming you are on the XZ palne, something like:

 var v3 = lighthouse.transform.position - ship.transform.position;
 angle = Mathf.Atan2(v3.z, v3.x) * Mathf.Rad2Deg;  

This will produce an absolute angle where Vector3.right is be 0 and increase counter-clockwise. You can use Mathf.DeltaAngle() to calculate angle differences.

If you don't need the angle, but are just interested in whether the point is to the left or right of you, there are easier choices. Take a look at SignedDotProduct in the Math3D script in the Wiki.

Another solution by @Eric5h5 can be found here.

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 Michel Verheughe · Mar 22, 2013 at 09:45 AM 0
Share

Thank you very much. This works. But with a slight change: Atan works only with one angle. We must use Atan2. Then, because in my coordinate system, north is z getting smaller (contrary to the latitudes) and east (x) is positive (as in reality), I must correct for that and the correct bearing reading, in geographical coordinates is:

 var v3 = lighthouse.transform.position - ship.transform.position;
 angle = ($$anonymous$$athf.Atan2(-v3.z, v3.x) * $$anonymous$$athf.Rad2Deg) - 90;
 if (angle < 0) angle += 360;

I write this in case someone else has the same problem. Thanks again,

$$anonymous$$ichel

avatar image
0

Answer by UDN_db6b683c-fff9-40da-8b63-c7c0b9a2d7e3 · Sep 29, 2020 at 02:39 AM

Just to build off of @robertbu code, here's what I did:

 Vector3 targetDirection = (target.transform.position - source.transform.position);
 float angleToTarget = (Mathf.Atan2(targetDirection.z, targetDirection.x) * Mathf.Rad2Deg) - 90f;
 float heading = angleToTarget;
 if (angleToTarget >= -90f && angleToTarget < 0f)
 {
     // North-East
     Debug.Log("NE:" + angleToTarget);
     heading = heading * -1f;
 }
 else if (angleToTarget >= -180f && angleToTarget < -90f)
 {
     // South-East
     Debug.Log("SE:" + angleToTarget);
     heading = heading * -1f;
 }
 else if (angleToTarget >= -270f&& angleToTarget < -180f)
 {
     // South-West
     Debug.Log("SW:" + angleToTarget);
     heading = heading * -1f;
 }
 else if (angleToTarget >= 0f && angleToTarget < 90f)
 {
     // North-West
     Debug.Log("NW:" + angleToTarget);
     heading = (heading - 360f) * -1f;
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

How to reset camera angle when disabling/disabled 1 Answer

Get the gameobject to the left/right of current target, in field of vision? 0 Answers

calculate the angle live 0 Answers

Sliding game slope jittering. 0 Answers

Convert vertical angle to horizontal angle (C#) 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