Advertisement
Guest User

Unity C# Hexagon Map Coordinate Utility

a guest
May 12th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MapCoordinateUtility {
  6.     public static Vector2Int getTileCoordinates(Vector3 pos) {
  7.         int x, y;
  8.         x = Mathf.RoundToInt(pos.x / 1.5f);
  9.         if (x % 2 == 0) {
  10.             y = Mathf.RoundToInt((pos.z - 0.866f) / 1.732f);
  11.         } else {
  12.             y = Mathf.RoundToInt(pos.z / 1.732f);
  13.         }
  14.         return new Vector2Int(x, y);
  15.     }
  16.     public static Vector3 getTileCoordinates(int x, int y) {
  17.         Vector3 newCoords = new Vector3(1.5f * (float)x, 0f, 1.732f * (float)y);
  18.         if (x % 2 == 0) {
  19.             newCoords += new Vector3(0f, 0f, 0.866f);
  20.         }
  21.         return newCoords;
  22.     }
  23.     public static Vector3 getTileCoordinates(Vector2Int coords) {
  24.         return getTileCoordinates(coords.x, coords.y);
  25.     }
  26.     public static Vector3 getMapLocationFromScreenPoint(Camera camera, Vector3 screenCoords) {
  27.         //https://answers.unity.com/questions/269760/ray-finding-out-x-and-z-coordinates-where-it-inter.html
  28.         Ray ray = camera.ScreenPointToRay(screenCoords);
  29.         //Create a plane at 0,0,0 whose normal points to +Y:
  30.         Plane hPlane = new Plane(Vector3.up, Vector3.zero);
  31.         //Plane.Raycast stores the distance from ray.origin to the hit point in this variable:
  32.         float distance = 0;
  33.         //If the ray hits the plane...
  34.         if (hPlane.Raycast(ray, out distance)) {
  35.             // get the hit point:
  36.             return ray.GetPoint(distance);
  37.         }
  38.         return Vector3.positiveInfinity;
  39.     }
  40.     public static Vector2Int[] getNeighborCoordsOfLocation(Vector2Int neighborCenter) {
  41.         List<Vector2Int> neighborCoords = new List<Vector2Int>();
  42.         //Directly Above
  43.         Vector2Int directlyAbove = new Vector2Int(neighborCenter.x, neighborCenter.y + 1);
  44.         neighborCoords.Add(directlyAbove);
  45.         //Directly Below
  46.         Vector2Int directlyBelow = new Vector2Int(neighborCenter.x, neighborCenter.y - 1);
  47.         neighborCoords.Add(directlyBelow);
  48.         //Upper Right
  49.         Vector2Int upperRight;
  50.         if (neighborCenter.x % 2 == 0) {
  51.             upperRight = new Vector2Int(neighborCenter.x + 1, neighborCenter.y + 1);
  52.         } else {
  53.             upperRight = new Vector2Int(neighborCenter.x + 1, neighborCenter.y);
  54.         }
  55.         neighborCoords.Add(upperRight);
  56.         //Lower Right
  57.         Vector2Int lowerRight;
  58.         if (neighborCenter.x % 2 == 0) {
  59.             lowerRight = new Vector2Int(neighborCenter.x + 1, neighborCenter.y);
  60.         } else {
  61.             lowerRight = new Vector2Int(neighborCenter.x + 1, neighborCenter.y - 1);
  62.         }
  63.         neighborCoords.Add(lowerRight);
  64.         //Upper Left
  65.         Vector2Int upperLeft;
  66.         if (neighborCenter.x % 2 == 0) {
  67.             upperLeft = new Vector2Int(neighborCenter.x - 1, neighborCenter.y + 1);
  68.         } else {
  69.             upperLeft = new Vector2Int(neighborCenter.x - 1, neighborCenter.y);
  70.         }
  71.         neighborCoords.Add(upperLeft);
  72.         //Lower Left
  73.         Vector2Int lowerLeft;
  74.         if (neighborCenter.x % 2 == 0) {
  75.             lowerLeft = new Vector2Int(neighborCenter.x - 1, neighborCenter.y);
  76.         } else {
  77.             lowerLeft = new Vector2Int(neighborCenter.x - 1, neighborCenter.y - 1);
  78.         }
  79.         neighborCoords.Add(lowerLeft);
  80.  
  81.         return neighborCoords.ToArray();
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement