Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ibrahimAlfors · Apr 30, 2018 at 09:31 PM · unity 5locationpointgpscompass

GPS 2D compass to point to Longitude and Latitude

Hello everyone, I want to ask you about something. I have this project that is required to use the GPS coordinates from the phone and the direction to implement a compass to point to other fixed coordinates.

I have searched for a while and the closest I got to what I need was this link: https://stackoverflow.com/questions/23207486/making-a-compass-point-to-a-particular-location-in-unity

I'm won't say I understand the equation mentioned in the link but all it did was make the object point downwards no matter where I'm or how the phone is oriented.

Can someone guide me through this or point me to a tutorial on how to implement such feature?

Thank you.

[Edit] I have found a website explaining an equation that would calculate the bearing and the distance between two points but I'm still struggling with adding it to my scripts https://www.movable-type.co.uk/scripts/latlong.html

Comment
Add comment · Show 1
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 TanselAltinel · May 02, 2018 at 08:12 PM 0
Share

Do you need to understand the calculation? Because I've worked with GIS systems for so long and all calculations are used as they are, just usually translated into program$$anonymous$$g language that is being used at the specific project.

Did you try the calculation in the link you provided? Did you take the answer into consideration?

1 Reply

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

Answer by ibrahimAlfors · May 02, 2018 at 10:29 PM

I was able to find a solution for those who want to use it, I used this link https://stackoverflow.com/questions/3932502/calculate-angle-between-two-latitude-longitude-points

here is my code

 private float angleFromCoordinate(float lat1, float long1, float lat2, float long2) {
         lat1 *= Mathf.Deg2Rad;
         lat2 *= Mathf.Deg2Rad;
         long1 *= Mathf.Deg2Rad;
         long2 *= Mathf.Deg2Rad;
 
         float dLon = (long2 - long1);
         float y = Mathf.Sin(dLon) * Mathf.Cos(lat2); 
         float x = (Mathf.Cos(lat1) * Mathf.Sin(lat2)) - (Mathf.Sin(lat1) * Mathf.Cos(lat2) * Mathf.Cos(dLon));
         float brng = Mathf.Atan2(y, x); 
         brng = Mathf.Rad2Deg* brng; 
         brng = (brng + 360) % 360; 
         brng = 360 - brng;
         return brng;
     }

and to use it, in the Update Method:

 float bearing = angleFromCoordinate(current.latitude,current.longitude,
             destination.latitude,destination.longitude);
         
         compass.rotation = Quaternion.Slerp(compass.rotation, Quaternion.Euler(0, 0, Input.compass.magneticHeading + bearing), 100f);

I hope this help you guys.

Thanks.

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 ibrahimAlfors · May 02, 2018 at 10:30 PM 0
Share

One strange thing thou, if you flip your phone on its screen, it also flips the compass and makes the bearing inverted. Not sure how to fix that thou.

avatar image Eeyung · Oct 12, 2018 at 04:40 AM 0
Share

Hi, may i know how to apply it to a gameobject? let's say i want an arrow(game object) always point to the destination coordinate during runtime, it is allow to apply it to a game object?

avatar image ibrahim2unityfocus Eeyung · Oct 13, 2018 at 11:59 AM 0
Share

Hello, Well, as you see in the answer all you need is to create the method angleFromCoordinate and use the other code snippet in the update method. You will need to enable the GPS. After that, just drag and drop the script on the object and it should work.

avatar image ABHINAVSAHA_- · Oct 17, 2020 at 07:54 PM 0
Share

The script doesn't seems to be working for me. The compass rotates a bit and doesn't rotates any further. It doesn't rotates when I rotate my phone

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

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

Why is compass heading affected by acceleration? 0 Answers

location base vr 0 Answers

Length between GPS-locations 1 Answer

MapNav -error -"Please enable location services and restart app" 0 Answers

How to consistently check if GPS is enabled 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