- Home /

# 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

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 programming 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?

**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.

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.

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?

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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

location base vr 0 Answers

Get Real GPS not WiFi GPS 2 Answers

Unity3D - Get smooth speed and acceleration with GPS data 2 Answers

Radius around GPS location 1 Answer

GPS location string 0 Answers