Created
October 27, 2023 20:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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