Skip to content

Instantly share code, notes, and snippets.

@TheMehranKhan
Created October 27, 2023 20:36
Show Gist options
  • Save TheMehranKhan/1b13a66d9ba1606003619d94e3da7cfe to your computer and use it in GitHub Desktop.
Save TheMehranKhan/1b13a66d9ba1606003619d94e3da7cfe to your computer and use it in GitHub Desktop.
A location-based game that uses Mapbox to check if the player is within a target radius.
using UnityEngine;
using Mapbox.Unity.Map;
using Mapbox.Unity.Location;
/// <summary>
/// A location-based game that uses Mapbox to check if the player is within a target radius.
/// </summary>
public class LocationBasedGame : MonoBehaviour
{
/// <summary>
/// The target latitude coordinate.
/// </summary>
public float targetLatitude;
/// <summary>
/// The target longitude coordinate.
/// </summary>
public float targetLongitude;
/// <summary>
/// The target radius in meters.
/// </summary>
public float targetRadius = 10f;
private MapboxLocationProvider locationProvider;
void Start()
{
// Initialize the location provider
locationProvider = new MapboxLocationProvider();
locationProvider.Start();
}
void Update()
{
// Get the player's current location
Location playerLocation = locationProvider.CurrentLocation;
// Check if the player is within the target radius
if (IsPlayerWithinTargetRadius(playerLocation))
{
// Player is within the target radius, do something
Debug.Log("Player is within target radius!");
}
}
/// <summary>
/// Checks if the player is within the target radius.
/// </summary>
/// <param name="playerLocation">The player's current location.</param>
/// <returns>True if the player is within the target radius, false otherwise.</returns>
bool IsPlayerWithinTargetRadius(Location playerLocation)
{
// Calculate the distance between player and target
float distance = Vector2.Distance(playerLocation.coordinate, new Vector2(targetLatitude, targetLongitude));
// Check if the distance is within the target radius
return distance <= targetRadius;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment