I have been looking all over and can’t seem to find the answer to what is probably a very simple problem. How can I calculate if a number is NEAR another number?
I want to use GPS coordinates in a game, and if a person is close enough to that number trigger an event. I know I could get 4 coordinates and do if(lat > 39.122 lat < 39.133) but it seems convoluted.
Is there a math function that tests or calculates if lat is within .10 of 39.122? I imagine it would involve PI or some other kind of math that I am really not good at!
Better yet, can I take both longitude and latitude and calculate if we are within X radius around the point?
I want the game to trigger easily if someone is near a point since I know the older iPhone’s are not that accurate so would like to adjust the range.
Hmm, that would calculate in the space of the game, right? I don’t actually need a character controller or anything, I just want to know if the iPhone location is near a specific point based on real world longitude and latitude. Sort of a geocaching application.
That looks very cool! Thanks everyone for all the help. In case someone is doing anything similar or wants to improve my code, here is the code that I have that seems to work for my needs.
This is for an application where the player must walk around an area, sort of like an Easter Egg hunt, and when they hit the target area they are rewarded. I plan to have about 20 targets, which are longitude and lattitude based within about a square mile. Using Vector2.Distance, I should be able to give an audio clue when people are getting close to the target.
I am not sure how to continuously ping for a location, so the way it works is each time the person touches the screen, it generates a new coordinate. I don’t understand why I have to Start and Stop in the getLocation, seems like I could start it up once and it would work, but it doesn’t. It does work with this code. (This is in 3.0 beta4).
I am attaching the script to a GUI Text object, and the reward object is dragged into the preFab variable.
I also don’t know if this is as accurate as it could be. I would be curious if factoring in the Haversine formula would make this notably more accurate. Right now, it seems to detect a person about 20 feet away from a target coordinate. I still get some false positives as I move away, but for my needs it should work OK.
The code below is using examples from the docs, and I’m printing out the location to aid in debugging and finding coordinates.
var stringToEdit : String = "Hello World\nI've got 2 lines...";
var preFab : GameObject;
var targetCoordinates : Vector2;
var iPhoneCoordinates : Vector2;
var distanceFromTarget = 0.00004;
private var proximity = 1.0;
function OnGUI () {
// Make a multiline text area that modifies stringToEdit.
stringToEdit = GUI.TextArea (Rect (10, 10, 200, 100), stringToEdit, 200);
}
function Start () {
//These are two hard-coded positions just to get started with testing, change these to something near you or it will never work!
targetCoordinates = Vector2(39.75838,-104.897);
iPhoneCoordinates = Vector2(39.75818, -104.8972);
// Start service before querying location
iPhoneSettings.StartLocationServiceUpdates();
// Wait until service initializes
var maxWait : int = 20;
while (iPhoneSettings.locationServiceStatus ==
LocationServiceStatus.Initializing maxWait > 0) {
yield WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1) {
print("Timed out");
return;
}
// User denied access to device location
if (iPhoneSettings.locationServiceStatus == LocationServiceStatus.Failed) {
print("User denied access to device location");
return;
}
// Access granted and location value could be retrieved
else {
print("Location: " + iPhoneInput.lastLocation.latitude + " " +
iPhoneInput.lastLocation.longitude + " " +
iPhoneInput.lastLocation.altitude + " " +
iPhoneInput.lastLocation.horizontalAccuracy + " " +
iPhoneInput.lastLocation.timestamp);
}
// Stop service if there is no need to query location updates continuously
//iPhoneSettings.StopLocationServiceUpdates();
}
function Update(){
//print(Vector2.Distance(targetCoordinates,iPhoneCoordinates));
if(Input.GetMouseButton(0)){
getLocation();
stringToEdit = "Latitude: " + iPhoneInput.lastLocation.latitude + "\nLongitude: " +
iPhoneInput.lastLocation.longitude + "\nAltitude: " +
iPhoneInput.lastLocation.altitude + "\nHorizontal Accuracy: " +
iPhoneInput.lastLocation.horizontalAccuracy + "\nTime: " +
iPhoneInput.lastLocation.timestamp;
//Here is where I start testing for the users position, first get the latest position
iPhoneCoordinates = Vector2(iPhoneInput.lastLocation.latitude,iPhoneInput.lastLocation.longitude);
//Find how close the user is to the target coordinates
proximity = Vector2.Distance(targetCoordinates,iPhoneCoordinates);
//If they are within this distance, give them a prize!
if (proximity < distanceFromTarget) {
Instantiate(preFab,Vector3(0,0,0), Quaternion.identity);
}
}
}
function getLocation(){
// Start service before querying location
iPhoneSettings.StartLocationServiceUpdates();
// Wait until service initializes
var maxWait : int = 20;
while (iPhoneSettings.locationServiceStatus ==
LocationServiceStatus.Initializing maxWait > 0) {
yield WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1) {
print("Timed out");
return;
}
// User denied access to device location
if (iPhoneSettings.locationServiceStatus == LocationServiceStatus.Failed) {
print("User denied access to device location");
return;
}
// Stop service if there is no need to query location updates continuously
iPhoneSettings.StopLocationServiceUpdates();
}
OK, I’m working on refining this now. It seems to be very good at detecting my initial entry to the target area. I am testing this in my house, so I walk from one part of my house to the target area. It did not trigger the reward until I was about 6 feet from my target coordinates (this is using the iPad). That’s perfect for what I need, but I noticed it keeps triggering as I walk away. Finally, when I was at the other side of my house, about 40 feet, it stopped triggering.
Do people think this is simply an inaccuracy of the iPad GPS or is it my script? I’m thinking the Starting and Stopping of Location services may be a culprit, although it doesn’t make sense why it would work so well the first time I walk to the area…
Well, it’s two years later, but in case anyone sees this and is interested, it seems to me that the reason locationServices stops every time is because the getLocation() function, which is called when the user taps the screen, makes a call to StopLocationServiceUpdates (last line of the function). In the original series of commands that appear in the Start function, that line is [correctly] commented out.
FWIW.
Also note that the syntax for getting locationInfo is deprecated; the correct syntax can be found here: